:root {
  --ink: #3b2f3f;
  --night: #3a2b57;
  --paper: #fffaf3;
  --pink: #ff7aa5;
  --pink-deep: #ff4f8b;
  --sun: #ffe07a;
  --mint: #8fe3c6;
  --lav: #c9b3ff;
  --font: 'Fredoka', 'Baloo 2', 'Trebuchet MS', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Noto Sans SC', 'Noto Sans CJK SC', system-ui, sans-serif;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--night);
  color: var(--paper);
  font-family: var(--font);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none; /* no iOS long-press menu while a thumb holds a stick */
  overscroll-behavior: none;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: block;
  cursor: crosshair;
}
body.in-menu #game { cursor: default; }

/* ---------------------------------------------------------------- menus */
#ui {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(58, 43, 87, 0.5), rgba(40, 26, 70, 0.85));
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-y: auto;
  touch-action: pan-y;
  z-index: 10;
}
#ui.hidden { display: none; }
#ui.see-through { background: rgba(40, 26, 70, 0.55); }

.screen {
  width: min(760px, 100%);
  max-height: 100%;
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-align: center;
}
.screen.hidden { display: none; }
.screen:not(#scr-title) {
  background: rgba(40, 26, 70, 0.86);
  border: 3px solid rgba(255, 250, 243, 0.14);
  border-radius: 24px;
  padding: 18px;
}
@media (max-height: 520px) { .screen:not(#scr-title) { padding: 10px 12px; border-radius: 18px; } }

h2 {
  margin: 0;
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 700;
  letter-spacing: 0.5px;
  text-shadow: 0 3px 0 var(--ink);
}
.sub { margin: 0; opacity: 0.85; font-size: 15px; }
.sub.left { text-align: left; max-width: 620px; line-height: 1.45; }
.tag { margin: -6px 0 6px; font-size: clamp(14px, 3.4vw, 18px); opacity: 0.9; }
.hint { font-size: 13px; opacity: 0.75; margin: 0; }
.portrait-hint { display: none; }
@media (orientation: portrait) and (pointer: coarse) { .portrait-hint { display: block; } }

#title-canvas { width: min(640px, 92vw, 100vh); height: auto; }
.lang-btn {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 2;
}

.stack { display: flex; flex-direction: column; gap: 10px; width: min(320px, 100%); }
.row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; }
.fields { gap: 14px; }

.btn {
  font-family: inherit;
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 10px 22px;
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
  touch-action: manipulation;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.big { background: var(--pink); color: #fff; font-size: 21px; padding: 12px 28px; text-shadow: 0 2px 0 rgba(59, 47, 63, 0.4); }
.btn.ghost { background: transparent; color: var(--paper); border-color: rgba(255, 250, 243, 0.6); box-shadow: none; }
.btn.tiny { font-size: 13px; padding: 3px 10px; box-shadow: 0 2px 0 var(--ink); vertical-align: middle; }
.btn:disabled { opacity: 0.5; cursor: default; }

label { display: inline-flex; align-items: center; gap: 8px; font-size: 15px; }
label.check { cursor: pointer; }
input, select {
  font-family: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 6px 10px;
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
}
input[type='checkbox'] { width: 20px; height: 20px; accent-color: var(--pink-deep); }
#solo-name { width: 130px; }
#acct-name { width: 11em; max-width: 100%; } /* room for the whole "Pick a username" placeholder */
#solo-name[readonly] { opacity: 0.7; }

/* ---------------------------------------------------------------- username bar (title screen) */
.account {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 14px;
  border-radius: 18px;
  background: rgba(40, 26, 70, 0.72);
  border: 2px solid rgba(255, 250, 243, 0.14);
  max-width: min(520px, 100%);
}
.acct-form, .acct-me { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; font-size: 15px; }
.acct-form.hidden, .acct-me.hidden { display: none; }
#acct-user { color: var(--sun); font-size: 17px; }
.acct-msg { margin: 0; font-size: 13px; line-height: 1.35; opacity: 0.9; max-width: 460px; }
.acct-msg:empty { display: none; }
.acct-msg.warn { color: #ffd0a0; opacity: 1; }

.device-prompt {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 24px));
  z-index: 30;
  padding: 12px 16px;
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  border: 3px solid var(--ink);
  box-shadow: 0 5px 0 var(--ink);
  text-align: center;
  touch-action: manipulation;
}
.device-prompt.hidden { display: none; }
.device-prompt p { margin: 0 0 10px; font-size: 15px; line-height: 1.4; }
.device-prompt .btn.ghost { color: var(--ink); border-color: var(--ink); }
#join-code { width: 120px; text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }

.err { color: #ffb0c0; min-height: 1.2em; margin: 0; font-size: 15px; }
.err.info { color: inherit; opacity: 0.85; }

/* ---------------------------------------------------------------- character cards */
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; width: 100%; }
@media (max-width: 520px) { .cards { grid-template-columns: repeat(2, 1fr); } }
.card {
  font-family: inherit;
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 18px;
  padding: 8px 6px 10px;
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  touch-action: manipulation;
  transition: transform 0.12s;
}
.card canvas { width: 70px; height: 86px; }
.card .name { font-weight: 700; font-size: 18px; }
.card .kind { font-size: 12px; opacity: 0.75; }
.card .stats { display: grid; grid-template-columns: auto auto; gap: 1px 6px; font-size: 11px; margin-top: 4px; align-items: center; }
.card .beads { display: flex; gap: 2px; }
.card .beads i { width: 8px; height: 8px; border-radius: 50%; background: #e7e0ee; box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.06); }
.card .beads i.on { background: var(--c); box-shadow: inset 0 0 0 2.4px rgba(0, 0, 0, 0.15); }
.card.sel { background: #fff3c4; transform: translateY(-4px); box-shadow: 0 8px 0 var(--ink); }
.card.pop { animation: pop 0.25s; }
@keyframes pop { 50% { transform: translateY(-4px) scale(1.06); } }
.card canvas.clip { width: 100%; height: auto; aspect-ratio: 168 / 128; border-radius: 12px; background: #f3f0ff; margin-bottom: 2px; }

/* ---------------------------------------------------------------- colours + hats */
.style-row { display: flex; flex-wrap: wrap; gap: 8px 22px; justify-content: center; width: 100%; }
.style-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }
.style-label { font-size: 14px; font-weight: 600; opacity: 0.85; }
.style-name { font-size: 13px; color: var(--sun); min-width: 9em; text-align: left; }
.picks { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.swatch, .hat-pick {
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
  border: 3px solid var(--ink);
  box-shadow: 0 3px 0 var(--ink);
  transition: transform 0.1s;
  padding: 0;
}
.swatch {
  width: 32px; height: 32px; border-radius: 50%;
  background: conic-gradient(var(--a) 0 50%, var(--b) 0 75%, var(--c) 0);
}
.hat-pick {
  width: 42px; height: 34px; border-radius: 10px;
  background: var(--paper);
  display: grid; place-items: center;
}
.hat-pick canvas { width: 34px; height: 25px; }
.hat-pick.none::after { content: ''; width: 16px; height: 16px; border: 2.5px solid #b9b4c7; border-radius: 50%; background: linear-gradient(45deg, transparent 44%, #b9b4c7 44% 56%, transparent 56%); }
.swatch.sel, .hat-pick.sel { transform: translateY(-3px); box-shadow: 0 0 0 3px var(--sun), 0 5px 0 var(--ink); }
.hat-pick.sel { background: #fff3c4; }

/* ---------------------------------------------------------------- lobby */
.lobby-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: 100%; }
@media (max-width: 520px) { .lobby-list { grid-template-columns: repeat(2, 1fr); } }
.seat {
  border: 3px dashed rgba(255, 250, 243, 0.35);
  border-radius: 14px;
  padding: 8px;
  min-height: 58px;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  font-size: 14px;
}
.seat.taken { border-style: solid; border-color: var(--c); background: rgba(255, 255, 255, 0.07); }
.seat .dot { width: 16px; height: 16px; border-radius: 50%; background: var(--c); flex: none; box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.15); }
.seat b { display: block; font-size: 15px; }
.seat small { opacity: 0.75; }
.code { font-family: ui-monospace, monospace; letter-spacing: 4px; color: var(--sun); }
.waiting { opacity: 0.8; }
body:not(.is-host) .host-only { display: none !important; }
body:not(.is-client) .client-only { display: none !important; }

/* ---------------------------------------------------------------- how to play */
#scr-how { width: min(1060px, 100%); max-height: none; }
#scr-how h3 { margin: 6px 0 0; font-size: 20px; color: var(--sun); text-shadow: 0 2px 0 var(--ink); }
.demo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 12px; width: 100%; }
.demo-card {
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(255, 250, 243, 0.14);
  border-radius: 16px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}
.demo-card canvas { width: 100%; height: auto; display: block; border-radius: 10px; }
.demo-text { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; line-height: 1.4; padding: 0 4px 2px; }
.demo-text b { color: var(--sun); margin-right: 3px; }
.chip-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--c); flex: none; margin-top: 3px; box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.15); }

/* ---------------------------------------------------------------- results */
.results { display: flex; flex-direction: column; gap: 8px; width: min(440px, 100%); }
.result {
  display: flex; align-items: center; gap: 12px;
  background: rgba(255, 255, 255, 0.08);
  border: 3px solid var(--c);
  border-radius: 14px;
  padding: 6px 12px;
  text-align: left;
}
.result canvas { width: 36px; height: 44px; }
.result .who { flex: 1; font-weight: 600; font-size: 17px; }
.result .num { font-size: 14px; opacity: 0.85; }
.result.win { background: rgba(255, 224, 122, 0.18); }

/* ---------------------------------------------------------------- short (landscape phone) screens */
@media (max-height: 520px) {
  .screen { gap: 8px; }
  #title-canvas { width: min(420px, 92vw, 90vh); }
  .stack.single { display: flex; }
  .tag { margin: -6px 0 0; font-size: 14px; }
  .account { padding: 5px 10px; gap: 2px; }
  .stack { display: grid; grid-template-columns: 1fr 1fr; width: min(500px, 100%); gap: 8px; }
  .stack .btn.big { grid-column: 1 / -1; }
  .btn { font-size: 15px; padding: 7px 16px; }
  .btn.big { font-size: 18px; padding: 8px 22px; }
  h2 { font-size: 22px; }
  .sub { font-size: 13px; }
  .card { padding: 5px 4px 7px; border-radius: 14px; }
  .card canvas { width: 44px; height: 54px; }
  .card canvas.clip { width: auto; height: 66px; }
  .swatch { width: 26px; height: 26px; }
  .hat-pick { width: 36px; height: 28px; }
  .hat-pick canvas { width: 28px; height: 21px; }
  .style-name { font-size: 12px; min-width: 0; }
  .card .name { font-size: 15px; }
  .card .kind { font-size: 11px; }
  .card .stats { margin-top: 2px; font-size: 10px; }
  .card .beads i { width: 7px; height: 7px; }
  .seat { min-height: 44px; padding: 5px 8px; font-size: 13px; }
  label { font-size: 14px; }
  input, select { font-size: 15px; padding: 4px 8px; }
  .demo-grid { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
  .demo-text { font-size: 12px; }
  #scr-how h3 { font-size: 17px; }
  .sub.left { font-size: 12px; }
}

/* ---------------------------------------------------------------- in-game buttons */
.round-btn {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: rgba(40, 26, 70, 0.55);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  z-index: 5;
  touch-action: manipulation;
}
body.in-menu #btn-menu { display: none; }

/* super power pop-up */
#power-pop {
  position: fixed;
  top: max(58px, calc(env(safe-area-inset-top) + 48px));
  left: 50%;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 12px;
  width: min(420px, calc(100vw - 24px));
  padding: 10px 14px;
  border-radius: 18px;
  border: 3px solid var(--c, var(--sun));
  background: rgba(40, 26, 70, 0.92);
  box-shadow: 0 6px 0 rgba(20, 12, 36, 0.5), 0 0 24px color-mix(in srgb, var(--c, #ffe07a) 45%, transparent);
  transform: translate(-50%, -24px) scale(0.92);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.3s cubic-bezier(0.3, 1.6, 0.5, 1);
}
#power-pop.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
#power-pop canvas { width: 52px; height: 52px; flex: none; }
#power-pop .pp-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; text-align: left; }
#power-pop .pp-name { font-size: 19px; color: var(--c, var(--sun)); text-shadow: 0 2px 0 var(--ink); }
#power-pop .pp-desc { font-size: 14px; line-height: 1.3; }
#power-pop .pp-time { font-size: 12px; font-weight: 700; opacity: 0.85; }
#power-pop .pp-bar { display: block; height: 5px; margin-top: 3px; border-radius: 3px; background: rgba(255, 255, 255, 0.18); overflow: hidden; }
#power-pop .pp-bar i { display: block; height: 100%; background: var(--c, var(--sun)); transform-origin: left; animation: pp-drain var(--dur, 6s) linear forwards; }
@keyframes pp-drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@media (max-height: 430px) {
  #power-pop { top: 8px; padding: 6px 10px; gap: 8px; }
  #power-pop canvas { width: 38px; height: 38px; }
  #power-pop .pp-name { font-size: 15px; }
  #power-pop .pp-desc { font-size: 12px; }
}

/* tutorial mode: which step you're on */
#tut-bar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  max-width: calc(100vw - 220px);
  padding: 5px 14px;
  border-radius: 999px;
  background: rgba(40, 26, 70, 0.85);
  border: 2px solid var(--pink);
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
#tut-bar.hidden { display: none; }

/* action buttons that ride along under your own beadie */
#player-actions {
  position: fixed;
  left: 0;
  top: 0;
  display: none;
  gap: 8px;
  z-index: 4;
  pointer-events: none;
  will-change: transform;
}
#player-actions.on { display: flex; }
#player-actions button {
  pointer-events: auto;
  position: relative;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.9);
  background: conic-gradient(rgba(40, 26, 70, 0.85) calc(var(--cd, 0) * 360deg), rgba(255, 122, 165, 0.78) 0);
  box-shadow: 0 2px 0 rgba(40, 26, 70, 0.6);
  display: grid;
  place-items: center;
  cursor: pointer;
  touch-action: none;
}
#player-actions button.ready { border-color: var(--sun); box-shadow: 0 0 0 2px rgba(255, 224, 122, 0.55), 0 2px 0 rgba(40, 26, 70, 0.6); }
#player-actions button:active { transform: scale(0.9); }
/* bomb in hand: its button pulses until you throw */
#player-actions.armed [data-pa='nade'] { animation: armed 0.5s ease-in-out infinite alternate; border-color: var(--sun); }
@keyframes armed { from { box-shadow: 0 0 0 2px rgba(255, 224, 122, 0.4); } to { box-shadow: 0 0 0 6px rgba(255, 224, 122, 0.85); } }
#player-actions .hint {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  font: 600 11px var(--font);
  color: var(--paper);
  background: rgba(40, 26, 70, 0.82);
  padding: 2px 9px;
  border-radius: 999px;
  pointer-events: none;
}
#player-actions.armed .hint { display: block; }
#player-actions svg { width: 24px; height: 24px; }
#player-actions kbd {
  position: absolute;
  right: -7px;
  bottom: -5px;
  min-width: 14px;
  padding: 0 3px;
  border-radius: 6px;
  border: 1.5px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  font: 700 9px/12px var(--font);
  text-align: center;
}
body.touch #player-actions kbd { display: none; }
/* reload is a key (R) on keyboards, so its button is only on touch screens */
#player-actions .pa-reload { display: none; width: 30px; height: 30px; align-self: center; font: 700 14px var(--font); color: #fff; }
body.touch #player-actions .pa-reload { display: grid; }
@media (max-height: 430px) {
  #player-actions { gap: 6px; }
  #player-actions button { width: 36px; height: 36px; }
  #player-actions svg { width: 21px; height: 21px; }
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 40px);
  opacity: 0;
  background: var(--paper);
  color: var(--ink);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 600;
  transition: 0.25s;
  pointer-events: none;
  z-index: 20;
  max-width: 90vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
