:root {
  --bg: #0f1419;
  --bg-card: #1a2332;
  --bg-elevated: #243044;
  --text: #e8eef7;
  --muted: #8b9bb4;
  --accent: #3d9cf0;
  --accent-dim: #2a6fad;
  --gold: #f0c14b;
  --gold-dim: #b8922e;
  --danger: #e85d5d;
  --success: #3ecf8e;
  --warning: #f0a030;
  --radius: 16px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --app-max: 520px;
  --play-actions-h: 5.5rem;
  --comms-bg: #1e3a56;
  --comms-bg-elevated: #2a4d6e;
  --comms-bg-deep: #173049;
  --comms-border: rgba(120, 185, 240, 0.32);
  --comms-shadow: 0 10px 36px rgba(20, 60, 100, 0.45), 0 0 0 1px rgba(100, 175, 235, 0.12);
  --card-w: 3.1rem;
  --card-h: 4.4rem;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(61, 156, 240, 0.18), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(240, 193, 75, 0.08), transparent),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
  border: none;
  border-radius: 12px;
  padding: 0.85rem 1.25rem;
  font-weight: 600;
  transition: transform 0.12s ease, opacity 0.12s ease, background 0.12s ease;
}

button:active:not(:disabled) {
  transform: scale(0.97);
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: #fff;
  width: 100%;
  box-shadow: 0 4px 16px rgba(61, 156, 240, 0.35);
}

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.btn-ghost-sm,
.btn-sm,
.btn-copy-link,
.btn-copy-icon,
.btn-danger-sm {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text);
  width: auto;
}

.btn-danger-sm {
  background: rgba(232, 93, 93, 0.2);
  color: var(--danger);
  border: 1px solid rgba(232, 93, 93, 0.4);
}

.app {
  max-width: var(--app-max);
  margin: 0 auto;
  padding: 1rem 1rem 2rem;
  min-height: 100vh;
}

.brand {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.brand h1 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: linear-gradient(135deg, #fff 0%, var(--accent) 55%, var(--gold) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.conn-status {
  font-size: 0.75rem;
  color: var(--muted);
}

.conn-status.is-live {
  color: var(--success);
}

.conn-status.is-down {
  color: var(--danger);
}

.hidden {
  display: none !important;
}

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1.15rem 1.2rem;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.05);
  margin-bottom: 1rem;
}

.card h2 {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.hint,
.muted {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.field {
  margin-bottom: 0.9rem;
}

.field label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.field input,
.field select {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: var(--bg);
  color: var(--text);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.divider {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0.5rem 0;
}

.home-history-link {
  text-align: center;
  margin-top: 1.25rem;
}

.home-history-link a {
  color: var(--muted);
  font-size: 0.9rem;
}

.color-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.color-swatch {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}

.color-swatch.is-selected {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--accent);
}

.color-swatch.is-taken {
  opacity: 0.35;
  cursor: not-allowed;
}

.public-games-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.public-games-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.public-games-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.public-games-list li:last-child {
  border-bottom: none;
}

.visibility-options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.radio-label,
.checkbox-label {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
}

.room-code-box {
  text-align: center;
  padding: 1rem;
  margin-bottom: 1rem;
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid rgba(240, 193, 75, 0.25);
}

.room-code-box .label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

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

.invite-link-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  justify-content: center;
  margin-top: 0.5rem;
}

.invite-link {
  font-size: 0.75rem;
  color: var(--muted);
  word-break: break-all;
  max-width: 70%;
}

.player-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.player-list li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.avatar-dot {
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  background: var(--bg-elevated);
  color: var(--muted);
  margin-left: auto;
}

.badge.ready {
  background: rgba(62, 207, 142, 0.2);
  color: var(--success);
}

.badge.host {
  background: rgba(240, 193, 75, 0.2);
  color: var(--gold);
}

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

.lobby-btn-row .btn-primary,
.lobby-btn-row .btn-secondary {
  width: 100%;
}

.game-start-banner,
.vote-kick-banner,
.spectator-banner {
  padding: 0.65rem 0.9rem;
  border-radius: 12px;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}

.game-start-banner {
  background: rgba(61, 156, 240, 0.2);
  border: 1px solid rgba(61, 156, 240, 0.4);
}

.vote-kick-banner {
  background: rgba(240, 160, 48, 0.15);
  border: 1px solid rgba(240, 160, 48, 0.4);
}

.spectator-banner {
  background: rgba(139, 155, 180, 0.15);
  border: 1px solid rgba(139, 155, 180, 0.3);
}

/* Play */
#screen-play {
  padding-bottom: calc(var(--play-actions-h) + 2rem);
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  margin-bottom: 0.65rem;
  color: var(--muted);
}

.top-bar-right {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.turn-banner {
  text-align: center;
  font-weight: 700;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  border-radius: 10px;
  background: var(--bg-elevated);
}

.turn-banner.is-you {
  background: rgba(62, 207, 142, 0.18);
  color: var(--success);
}

.last-action {
  text-align: center;
  margin-bottom: 0.65rem;
  min-height: 1.2em;
}

.scores-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.score-chip {
  font-size: 0.8rem;
  padding: 0.3rem 0.55rem;
  border-radius: 999px;
  background: var(--bg-card);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.score-chip.is-turn {
  border-color: var(--success);
}

.opponents {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.opponent-seat {
  flex: 1;
  min-width: 5rem;
  background: var(--bg-card);
  border-radius: 12px;
  padding: 0.5rem;
  text-align: center;
  font-size: 0.8rem;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.opponent-seat.is-turn {
  border-color: var(--success);
}

.table-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

.table-cards,
.hand-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  min-height: var(--card-h);
  align-items: flex-start;
}

.playing-card {
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 8px;
  background: #f7f4ef;
  color: #1a1a1a;
  border: 2px solid transparent;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  user-select: none;
  position: relative;
  padding: 0;
}

.playing-card.red {
  color: #c0392b;
}

.playing-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent), 0 4px 12px rgba(61, 156, 240, 0.45);
  transform: translateY(-4px);
}

.playing-card.special::after {
  content: "★";
  position: absolute;
  top: 2px;
  right: 3px;
  font-size: 0.55rem;
  color: var(--gold-dim);
}

.playing-card .suit {
  font-size: 1rem;
  line-height: 1;
}

.build-card {
  width: auto;
  min-width: calc(var(--card-w) * 1.35);
  height: var(--card-h);
  border-radius: 8px;
  background: linear-gradient(145deg, #2a3d55, #1a2838);
  border: 2px solid var(--gold);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  cursor: pointer;
  padding: 0.25rem;
}

.build-card.selected {
  box-shadow: 0 0 0 2px var(--accent);
}

.build-card .build-value {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--gold);
}

.card-back {
  width: 1.4rem;
  height: 2rem;
  border-radius: 4px;
  background: linear-gradient(135deg, #2a5a8c, #1a3a5c);
  border: 1px solid rgba(255, 255, 255, 0.15);
  display: inline-block;
  margin: 0 1px;
}

.hand-area {
  margin-top: 0.75rem;
  padding: 0.75rem;
  background: var(--bg-card);
  border-radius: var(--radius);
}

.play-actions {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: var(--app-max);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr auto;
  gap: 0.4rem;
  padding: 0.65rem 0.85rem calc(0.65rem + env(safe-area-inset-bottom));
  background: rgba(15, 20, 25, 0.92);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  z-index: 20;
}

.play-actions .btn-primary,
.play-actions .btn-secondary {
  width: 100%;
  padding: 0.75rem 0.4rem;
  font-size: 0.9rem;
}

.host-play-row {
  position: fixed;
  right: max(0.5rem, calc(50% - var(--app-max) / 2 + 0.5rem));
  bottom: calc(var(--play-actions-h) + 0.25rem);
  z-index: 21;
}

.standings {
  margin: 0;
  padding-left: 1.25rem;
}

.standings li {
  margin: 0.4rem 0;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 6rem;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 0.65rem 1rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 50;
  max-width: 90%;
  font-size: 0.9rem;
}

.toast.is-error {
  border: 1px solid var(--danger);
}

/* Comms */
.comms-root {
  position: fixed;
  z-index: 30;
}

#chat-root {
  right: max(0.75rem, calc(50% - var(--app-max) / 2 + 0.75rem));
  bottom: calc(var(--play-actions-h) + 3.5rem);
}

.voice-root {
  right: max(0.75rem, calc(50% - var(--app-max) / 2 + 0.75rem));
  bottom: calc(var(--play-actions-h) + 7rem);
}

#screen-home ~ .comms-root,
body:has(#screen-home:not(.hidden)) .comms-root {
  /* shown via JS */
}

.comms-fab {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background: var(--comms-bg);
  border: 1px solid var(--comms-border);
  box-shadow: var(--comms-shadow);
  font-size: 1.25rem;
  padding: 0;
}

.comms-panel {
  position: absolute;
  right: 0;
  bottom: 3.5rem;
  width: min(20rem, calc(100vw - 2rem));
  max-height: 22rem;
  display: flex;
  flex-direction: column;
  background: var(--comms-bg);
  border: 1px solid var(--comms-border);
  border-radius: 14px;
  box-shadow: var(--comms-shadow);
  overflow: hidden;
}

.comms-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.55rem 0.75rem;
  background: var(--comms-bg-deep);
  border-bottom: 1px solid var(--comms-border);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.6rem;
  min-height: 10rem;
  max-height: 14rem;
  font-size: 0.85rem;
}

.chat-msg {
  margin-bottom: 0.45rem;
  line-height: 1.35;
}

.chat-msg .who {
  font-weight: 700;
  margin-right: 0.35rem;
}

.chat-form {
  display: flex;
  gap: 0.35rem;
  padding: 0.5rem;
  border-top: 1px solid var(--comms-border);
}

.chat-form input {
  flex: 1;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--comms-bg-deep);
  color: var(--text);
}

.voice-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.65rem;
}

.voice-controls button {
  width: auto;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
}

.voice-peers {
  list-style: none;
  margin: 0;
  padding: 0 0.65rem 0.65rem;
  font-size: 0.85rem;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 1rem;
}

.modal-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1.25rem;
  width: min(20rem, 100%);
  box-shadow: var(--shadow);
}

.modal-card h3 {
  margin: 0 0 0.5rem;
}

.modal-card input[type="number"] {
  width: 100%;
  padding: 0.65rem;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--bg);
  color: var(--text);
}
