:root {
  --bg: #0e1130;
  --bg2: #171b46;
  --card: #1c2152;
  --card-edge: #2c3373;
  --text: #eef0ff;
  --muted: #9aa2d8;
  --accent: #5ef0c8;
  --accent-ink: #062b22;
  --pink: #ff5470;
  --gold: #ffcc33;
  --blue: #36b3ff;
  --danger: #ff6b81;
  --radius: 18px;
  font-family: 'Fredoka', ui-rounded, 'Segoe UI', system-ui, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, #2a2f7a 0%, transparent 60%),
    radial-gradient(900px 500px at 110% 110%, #1e5a6b 0%, transparent 55%),
    var(--bg);
  background-attachment: fixed;
}

body { overflow-x: hidden; }

button { font-family: inherit; }

.hidden { display: none !important; }

.screen { display: none; min-height: 100vh; min-height: 100dvh; padding: 16px; }
.screen.active { display: flex; align-items: center; justify-content: center; }
#screen-play.active { display: block; padding: 12px 12px 0; }

.card {
  background: linear-gradient(180deg, var(--card), #161a42);
  border: 1px solid var(--card-edge);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  width: 100%;
}

/* ---------- join ---------- */
.join-card { max-width: 420px; text-align: center; }
.logo { margin: 0; font-size: 56px; line-height: 1; font-weight: 700; letter-spacing: 0.5px; }
.logo-dr { color: var(--accent); }
.logo-cleo {
  background: linear-gradient(90deg, var(--pink), var(--gold) 50%, var(--blue));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.tagline { color: var(--muted); margin: 8px 0 20px; }

form { display: grid; gap: 14px; text-align: left; }
label { display: grid; gap: 6px; font-weight: 500; color: var(--muted); }
input {
  font: inherit;
  font-size: 18px;
  color: var(--text);
  background: #0f1234;
  border: 1px solid var(--card-edge);
  border-radius: 12px;
  padding: 10px 12px;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
#room { text-transform: uppercase; letter-spacing: 2px; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 18px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.primary:disabled { background: #3a4180; color: var(--muted); cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--card-edge); }
.btn.ghost:hover { color: var(--text); }
.btn.small { font-size: 14px; padding: 8px 12px; }
.btn.big { font-size: 22px; padding: 14px 28px; }

.error { color: var(--danger); min-height: 1.2em; margin: 0; }
.hint { color: var(--muted); font-size: 14px; margin: 6px 0; }

.howto { margin-top: 16px; text-align: left; color: var(--muted); }
.howto summary { cursor: pointer; color: var(--text); font-weight: 500; }
.howto ul { padding-left: 18px; }
.howto li { margin: 6px 0; }

/* ---------- lobby ---------- */
.lobby-card { max-width: 560px; }
.lobby-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.lobby-head h2 { margin: 0; }
.room-code { color: var(--accent); letter-spacing: 3px; }

.players { list-style: none; padding: 0; margin: 18px 0 4px; display: grid; gap: 8px; }
.players li {
  display: flex; align-items: center; gap: 10px;
  background: #13173d; border: 1px solid var(--card-edge); border-radius: 12px; padding: 10px 14px;
}
.players .dot { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.players .pname { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.players .you { color: var(--accent); font-weight: 500; font-size: 14px; }
.players .meta { color: var(--muted); font-size: 14px; white-space: nowrap; }
.players .wins { color: var(--gold); font-weight: 600; white-space: nowrap; }

.my-settings { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--card-edge); }
.my-settings h3 { margin: 0 0 10px; }
.level-label { grid-template-columns: auto 1fr; align-items: center; }
.level-label output { color: var(--text); font-weight: 700; font-size: 20px; text-align: right; }
.level-label input { grid-column: 1 / -1; padding: 0; accent-color: var(--accent); }

.seg { display: flex; gap: 6px; margin-top: 12px; }
.seg button {
  flex: 1; font-size: 16px; font-weight: 600; padding: 10px; border-radius: 10px; cursor: pointer;
  background: #13173d; color: var(--muted); border: 1px solid var(--card-edge);
}
.seg button[aria-checked='true'] { background: var(--blue); color: #04213a; border-color: var(--blue); }

.lobby-actions { margin-top: 20px; text-align: center; }
.leave { display: block; margin: 16px auto 0; }

/* ---------- play ---------- */
.play {
  display: flex;
  gap: 20px;
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}
.me { display: flex; gap: 12px; align-items: flex-start; }
.board-wrap { position: relative; border-radius: 16px; }
.board-wrap.hit { animation: shake 0.35s ease; }
#board { display: block; filter: drop-shadow(0 10px 30px rgba(0, 0, 0, 0.45)); }

.side { display: grid; gap: 8px; width: 96px; }
.panel { background: rgba(28, 33, 82, 0.8); border: 1px solid var(--card-edge); border-radius: 12px; padding: 8px; text-align: center; }
.label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; }
.big-num { font-size: 30px; font-weight: 700; }
.num { font-size: 20px; font-weight: 600; }
#next { display: block; margin: 6px auto 2px; }
#hudIncoming.warn { color: var(--danger); }

.countdown, .pop-text, .board-msg {
  position: absolute; left: 0; right: 0; text-align: center; pointer-events: none; font-weight: 700;
}
.countdown { top: 38%; font-size: 84px; text-shadow: 0 6px 30px rgba(0, 0, 0, 0.7); }
.pop-text { top: 22%; font-size: 30px; color: var(--gold); text-shadow: 0 3px 12px rgba(0, 0, 0, 0.8); opacity: 0; }
.pop-text.show { animation: pop 1s ease forwards; }
.board-msg { top: 42%; font-size: 30px; text-shadow: 0 4px 18px rgba(0, 0, 0, 0.9); }

.opponents { display: grid; grid-template-columns: repeat(2, auto); gap: 12px; align-content: start; }
.opp {
  position: relative;
  background: rgba(28, 33, 82, 0.7); border: 1px solid var(--card-edge); border-radius: 14px; padding: 8px;
  transition: transform 0.2s ease;
}
.opp.sent { animation: sent 0.6s ease; }
.opp-head { display: flex; justify-content: space-between; gap: 6px; font-size: 14px; margin-bottom: 4px; }
.opp-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opp-wins { color: var(--gold); white-space: nowrap; }
.opp canvas { display: block; }
.opp-foot { font-size: 13px; color: var(--muted); margin-top: 4px; display: flex; justify-content: space-between; }
.opp-status {
  position: absolute; inset: 30% 0 auto; text-align: center; font-weight: 700; font-size: 16px;
  color: var(--danger); text-shadow: 0 2px 8px #000; display: none;
}
.opp.out .opp-status { display: block; }

.spectate { width: 100%; text-align: center; color: var(--muted); }

.touch { display: none; }
@media (hover: none) and (pointer: coarse) {
  .touch {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
    position: sticky;
    bottom: 0;
    padding: 10px 4px calc(10px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, transparent, var(--bg) 30%);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
  }
  .touch button {
    height: 56px; font-size: 26px; border-radius: 14px; border: 1px solid var(--card-edge);
    background: var(--card); color: var(--text); touch-action: none;
  }
  .touch button:active, .touch button.on { background: #2c3373; }
  .touch .wide { font-size: 15px; font-weight: 700; background: #23296a; }
}

/* narrow screens: opponents strip on top, board below */
@media (max-width: 760px) {
  .play { flex-direction: column; align-items: center; gap: 10px; }
  .opponents { order: -1; display: flex; gap: 6px; justify-content: center; flex-wrap: nowrap; }
  .opp { padding: 4px; border-radius: 10px; }
  .opp-head { font-size: 11px; }
  .opp-foot { font-size: 11px; }
  .side { width: 76px; }
  .big-num { font-size: 24px; }
  .num { font-size: 16px; }
  .logo { font-size: 46px; }
}

/* ---------- overlay & toasts ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(5, 7, 20, 0.6); display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 20; backdrop-filter: blur(3px);
}
.overlay-card { max-width: 380px; text-align: center; animation: rise 0.3s ease; }
.overlay-card h2 { font-size: 34px; margin: 0 0 8px; }

.toasts {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 30;
  display: grid; gap: 6px; width: min(92vw, 380px); pointer-events: none;
}
.toast {
  background: rgba(28, 33, 82, 0.95); border: 1px solid var(--card-edge); border-radius: 12px; padding: 8px 14px;
  text-align: center; font-weight: 500; animation: rise 0.25s ease;
}
.toast.good { border-color: var(--accent); }
.toast.bad { border-color: var(--danger); }

.mute {
  position: fixed; top: 10px; right: 10px; z-index: 25; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--card-edge); background: var(--card); color: var(--text); font-size: 18px; cursor: pointer;
}

@keyframes pop {
  0% { opacity: 0; transform: scale(0.6); }
  20% { opacity: 1; transform: scale(1.15); }
  70% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: translateY(-20px); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}
@keyframes sent {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255, 204, 51, 0.8); }
  50% { transform: scale(1.04); box-shadow: 0 0 0 6px rgba(255, 204, 51, 0.3); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 transparent; }
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition: none !important; }
}

/* ---------- Dr. Cléo character ---------- */
.cleo-wrap { display: block; position: relative; }
.cleo { position: relative; aspect-ratio: 4 / 5; transform-origin: 50% 100%; animation: cleo-bob 2.6s ease-in-out infinite; }
.cleo img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: contain; object-position: 50% 100%;
  opacity: 0; pointer-events: none; user-select: none;
}
[data-mood='happy'] .cleo-happy, [data-mood='wow'] .cleo-wow, [data-mood='worried'] .cleo-worried,
[data-mood='ko'] .cleo-ko, [data-mood='win'] .cleo-win { opacity: 1; }

[data-mood='wow'] .cleo { animation: cleo-hop 0.45s ease 2; }
[data-mood='win'] .cleo { animation: cleo-hop 0.6s ease infinite; }
[data-mood='worried'] .cleo { animation: cleo-jitter 0.25s linear infinite; }
[data-mood='ko'] .cleo { animation: none; transform: translateY(3px); transition: transform 0.4s ease; }

.cleo-hero { width: 180px; margin: -6px auto 4px; }
.cleo-hero::after {
  content: ''; display: block; width: 150px; height: 10px; margin: -4px auto 0; border-radius: 50%;
  background: radial-gradient(closest-side, rgba(0, 0, 0, 0.35), transparent);
}
.lobby-head .title { display: flex; align-items: center; gap: 10px; }
.cleo-mini { width: 56px; }
.cleo-panel { padding: 6px 4px 0; overflow: hidden; }
.cleo-panel .cleo-wrap { width: 84px; margin: 0 auto; }
.overlay-card .cleo-wrap { width: 150px; margin: 0 auto 8px; }

@media (max-width: 760px) {
  .cleo-panel .cleo-wrap { width: 64px; }
  .panel-level { display: none; }
}

@keyframes cleo-bob {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-3px) rotate(-1.5deg); }
}
@keyframes cleo-hop {
  0%, 100% { transform: translateY(0) scale(1); }
  40% { transform: translateY(-10px) scale(1.04, 0.97); }
}
@keyframes cleo-jitter {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-1.5px); }
  75% { transform: translateX(1.5px); }
}
