:root {
  --bg: #0d1726;
  --bg2: #15233a;
  --panel: #1b2b45;
  --panel2: #22375a;
  --line: rgba(255, 255, 255, 0.1);
  --text: #eef3fb;
  --muted: #8fa3c2;
  --accent: #3fb5ff;
  --gold: #f3c14b;
  --green: #45d06b;
  --red: #ff5a5a;
  --radius: 16px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body, #app { margin: 0; padding: 0; width: 100vw; height: 100vh; height: 100dvh; overflow: hidden; }
body {
  background: radial-gradient(120% 80% at 50% 0%, #1a2c4a 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

#app { position: fixed; inset: 0; display: flex; justify-content: center; }
#game { max-width: none; }
.hud, .toolbar, .tray-wrap { width: 100%; max-width: 640px; margin: 0 auto; }
.screen { width: 100%; max-width: 640px; height: 100%; display: flex; flex-direction: column; }

/* ---------- menu ---------- */
#menu { align-items: center; padding: 16px; overflow-y: auto; touch-action: pan-y; }
.menu-card {
  width: 100%; max-width: 380px; margin: auto 0; flex: none;
  background: linear-gradient(180deg, var(--panel) 0%, var(--bg2) 100%);
  border: 1px solid var(--line); border-radius: 24px;
  padding: 24px 20px 18px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  display: flex; flex-direction: column; gap: 12px;
}
.logo { text-align: center; }
.logo canvas { width: 80px; height: 80px; filter: drop-shadow(0 8px 14px rgba(0, 0, 0, 0.5)); }
.logo h1 { margin: 6px 0 0; font-size: 28px; letter-spacing: 0.5px; font-weight: 800; }
.logo h1 span { color: var(--gold); }
.tag { margin: 2px 0 6px; color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.8px; }
input {
  font: inherit; font-size: 16px; color: var(--text);
  background: #0f1c30; border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; outline: none; user-select: text; -webkit-user-select: text;
}
input:focus { border-color: var(--accent); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.btn {
  border: 1px solid var(--line); background: var(--panel2); border-radius: 12px;
  padding: 12px 14px; font-weight: 700; cursor: pointer; transition: transform 0.08s, filter 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn:hover { filter: brightness(1.12); }
.btn.primary { background: linear-gradient(180deg, #5ee07f, #2fae52); border-color: #2a9b49; color: #062010; box-shadow: 0 4px 0 #1f7a38; }
.btn.primary:active { box-shadow: 0 1px 0 #1f7a38; transform: translateY(3px); }
.btn.ghost { background: transparent; }
.btn.danger { background: linear-gradient(180deg, #ff7a7a, #d63d3d); border-color: #b02e2e; }
.join { display: flex; gap: 10px; }
.join input { flex: 1; text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 800; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; margin: 4px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.error { color: var(--red); min-height: 1.2em; margin: 0; text-align: center; font-size: 13px; }
.howto { color: var(--muted); font-size: 13px; }
.howto summary { cursor: pointer; text-align: center; }
.howto ul { padding-left: 18px; margin: 8px 0 0; }

/* ---------- HUD ---------- */
#game { padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); gap: 6px; }
.hud { display: flex; align-items: center; gap: 8px; }
.pcard {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px;
  background: linear-gradient(180deg, var(--panel) 0%, var(--bg2) 100%);
  border: 1px solid var(--line); border-radius: 14px; padding: 6px 8px;
  transition: box-shadow 0.2s, border-color 0.2s, opacity 0.2s;
  opacity: 0.72;
}
.pcard.right { flex-direction: row-reverse; text-align: right; }
.pcard.active { opacity: 1; border-color: rgba(69, 208, 107, 0.7); box-shadow: 0 0 0 1px rgba(69, 208, 107, 0.35), 0 0 18px rgba(69, 208, 107, 0.25); }
.pcard.offline .avatar span { filter: grayscale(1) brightness(0.6); }
.avatar { position: relative; width: 44px; height: 44px; flex: none; }
.avatar span {
  position: absolute; inset: 5px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 800; font-size: 16px;
  background: linear-gradient(140deg, #4f8dff, #2a4fd1); color: #fff;
}
.pcard.right .avatar span { background: linear-gradient(140deg, #ff9b4f, #d1452a); }
.avatar svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: rgba(255, 255, 255, 0.08); stroke-width: 3; }
.ring { fill: none; stroke: var(--green); stroke-width: 3; stroke-linecap: round; stroke-dasharray: 125.66; stroke-dashoffset: 125.66; transition: stroke 0.3s; }
.pinfo { min-width: 0; flex: 1; }
.pname { font-weight: 700; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.slots { display: flex; gap: 2px; margin-top: 3px; flex-wrap: nowrap; }
.pcard.right .slots { justify-content: flex-end; }
.slot { flex: 0 1 15px; min-width: 0; aspect-ratio: 1; border-radius: 50%; background: #0a1220; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.8); display: grid; place-items: center; }
.slot canvas { display: block; width: 100% !important; height: 100% !important; }
.vs { display: flex; flex-direction: column; align-items: center; gap: 2px; flex: none; }
.vsbadge {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 900; font-size: 12px; color: #1d1400;
  background: radial-gradient(circle at 35% 30%, #ffe8a0, var(--gold) 60%, #b8861a);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.4);
}
.roomcode { font-size: 10px; color: var(--muted); letter-spacing: 1px; }

.toolbar { display: flex; align-items: center; gap: 8px; }
.status { flex: 1; text-align: center; font-size: 13px; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.mine { color: var(--green); }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  display: grid; place-items: center; cursor: pointer; color: var(--muted); padding: 0;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:active { transform: scale(0.94); }
.spin-btn { width: 44px; height: 44px; border: none; background: none; padding: 0; cursor: pointer; }
.mini-cue, .big-cue {
  position: relative; display: block; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0%, #ecebe4 45%, #b9b7ad 100%);
  box-shadow: 0 0 0 3px #26354f, 0 4px 10px rgba(0, 0, 0, 0.45);
}
.mini-cue { width: 40px; height: 40px; margin: 2px; }
.mini-cue i, .big-cue i {
  position: absolute; left: 50%; top: 50%; width: 22%; height: 22%; border-radius: 50%;
  background: #e8262b; box-shadow: 0 0 0 2px rgba(232, 38, 43, 0.25);
  transform: translate(-50%, -50%);
}

.tray-wrap { display: flex; justify-content: center; }
.tray {
  min-height: 24px; min-width: 60%; display: flex; gap: 2px; align-items: center; justify-content: flex-start;
  padding: 3px 10px; border-radius: 14px;
  background: linear-gradient(180deg, #0a1322, #15233a);
  border: 1px solid var(--line); box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.6);
}

/* ---------- stage ---------- */
.stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; gap: 6px; }
.stage.portrait { align-items: flex-start; }
.canvas-wrap { position: relative; flex: none; display: flex; align-items: center; justify-content: center; }
#table { display: block; border-radius: 22px; box-shadow: 0 18px 40px rgba(0, 0, 0, 0.55); touch-action: none; }

.power { width: 42px; flex: none; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8% 0; cursor: ns-resize; touch-action: none; }
.power-track {
  position: relative; flex: 1; width: 30px; border-radius: 15px; overflow: hidden;
  background: linear-gradient(180deg, #0b1424, #18263d); border: 2px solid #2d4266;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.8);
}
.power-fill {
  position: absolute; left: 0; right: 0; top: 0; height: 0%;
  background: linear-gradient(180deg, #ffe25a 0%, #ffb02e 40%, #ff6a1f 75%, #e8262b 100%);
  background-size: 100% var(--track-h, 400px);
}
.power-cue {
  position: absolute; left: 50%; top: -6%; width: 8px; height: 100%; margin-left: -4px; border-radius: 4px;
  background: linear-gradient(180deg, #3b78c4 0 1.2%, #fff 1.2% 3.5%, #ecd7a8 3.5% 60%, #2a170c 60% 100%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
  transform: translateY(var(--pull, 0%));
}
.power-label { font-size: 9px; letter-spacing: 1.5px; color: var(--muted); font-weight: 800; }
.power.disabled, .fine.disabled { opacity: 0.35; pointer-events: none; }

.fine { width: 30px; flex: none; padding: 14% 0; touch-action: none; cursor: ns-resize; }
.fine-ridges {
  height: 100%; border-radius: 15px; border: 2px solid #2d4266;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.55), transparent 30%, transparent 70%, rgba(0, 0, 0, 0.55)),
    repeating-linear-gradient(180deg, #44587c 0 3px, #1a273d 3px 9px);
  background-position: 0 0, 0 var(--fine-y, 0px);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.6);
}

.toast {
  position: absolute; left: 50%; top: 14%; transform: translate(-50%, -50%) scale(0.9);
  background: rgba(8, 14, 26, 0.86); border: 1px solid var(--line); border-radius: 14px;
  padding: 10px 16px; font-weight: 700; text-align: center; max-width: 80%;
  opacity: 0; transition: opacity 0.25s, transform 0.25s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.toast.foul { border-color: rgba(255, 90, 90, 0.6); color: #ffd2d2; }
.bubble {
  position: absolute; top: 6px; padding: 6px 10px; border-radius: 12px; font-size: 13px; font-weight: 700;
  background: #fff; color: #10203a; opacity: 0; transform: translateY(-6px); transition: 0.2s; pointer-events: none;
}
.bubble.left { left: 8px; } .bubble.right { right: 8px; }
.bubble.show { opacity: 1; transform: none; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; background: rgba(4, 8, 16, 0.6); backdrop-filter: blur(4px); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal {
  width: 100%; max-width: 340px; background: linear-gradient(180deg, var(--panel), var(--bg2));
  border: 1px solid var(--line); border-radius: 20px; padding: 20px; text-align: center;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal h2 { margin: 0; font-size: 20px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.big-cue { width: 190px; height: 190px; margin: 6px auto; cursor: crosshair; touch-action: none; box-shadow: 0 0 0 5px #26354f, 0 10px 30px rgba(0, 0, 0, 0.5); }
.big-cue::before, .big-cue::after { content: ""; position: absolute; background: rgba(0, 0, 0, 0.12); }
.big-cue::before { left: 50%; top: 10%; bottom: 10%; width: 1px; }
.big-cue::after { top: 50%; left: 10%; right: 10%; height: 1px; }
.big-cue i { width: 18%; height: 18%; z-index: 1; }
.chat-modal { max-width: 360px; }
.chat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.spinner { width: 36px; height: 36px; margin: 4px auto; border-radius: 50%; border: 4px solid rgba(255, 255, 255, 0.12); border-top-color: var(--green); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.code-box p { margin: 0; color: var(--muted); font-size: 13px; }
.big-code { font-size: 40px; font-weight: 900; letter-spacing: 10px; margin: 4px 0 8px; color: var(--gold); }
.trophy { font-size: 0; height: 70px; display: grid; place-items: center; }
.trophy canvas { width: 70px; height: 70px; }

@media (min-width: 700px) {
  #game { padding-top: 14px; padding-bottom: 14px; }
  .pname { font-size: 15px; }
  .slot { flex-basis: 18px; }
}
@media (max-height: 640px) {
  .tray-wrap { display: none; }
}
/* short landscape players: tuck the HUD into one tighter row */
@media (max-height: 480px) {
  #game { gap: 4px; padding-top: 6px; padding-bottom: 6px; }
  .avatar { width: 36px; height: 36px; }
  .pcard { padding: 4px 6px; }
  .spin-btn { width: 36px; height: 36px; }
  .mini-cue { width: 32px; height: 32px; }
  .icon-btn { width: 32px; height: 32px; }
}

/* ---------- narrow vertical frames (phones, Yoodel Shorts) ---------- */
@media (max-width: 440px) {
  .stage { gap: 4px; }
  .power { width: 32px; }
  .power-track { width: 24px; }
  .power-label { font-size: 8px; letter-spacing: 1px; }
  .fine { width: 22px; }
  #game { padding-left: 6px; padding-right: 6px; }
  .hud { gap: 5px; }
  .pcard { gap: 5px; padding: 5px 6px; }
  .avatar { width: 38px; height: 38px; }
  .avatar span { font-size: 14px; }
  .vsbadge { width: 28px; height: 28px; font-size: 10px; }
}
/* short frames: compact menu so it fits without scrolling */
@media (max-height: 700px) {
  .menu-card { gap: 9px; padding: 16px 16px 12px; border-radius: 20px; }
  .logo canvas { width: 54px; height: 54px; }
  .logo h1 { font-size: 23px; margin-top: 2px; }
  .tag { margin-bottom: 2px; }
  .btn { padding: 10px 12px; }
  input { padding: 10px 12px; }
}

/* ---------- embedded in a player (Yoodel): keep the top-right corner free ---------- */
.embedded #game .toolbar { order: -1; padding-right: 92px; }
.embedded #soundBtn { display: none; } /* the host player owns volume */
.embedded #menu { padding-top: 52px; }
