/* Bi-a 8 bóng — phòng bi-a tối, nỉ xanh dương */
:root {
  --bg: #0b0e13;
  --panel: #161c26;
  --panel2: #1e2632;
  --line: rgba(255, 255, 255, 0.09);
  --text: #eef1f6;
  --muted: #9aa6b8;
  --gold: #ffd34d;
  --accent: #2b86d0;
  --red: #ff5d5d;
}
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}
[hidden] {
  display: none !important;
}
html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Be Vietnam Pro", system-ui, sans-serif;
  overscroll-behavior: none;
}
button {
  font: inherit;
  color: inherit;
}
.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(ellipse 70% 55% at 50% 42%, rgba(255, 214, 140, 0.13), transparent 70%),
    radial-gradient(ellipse at 50% 0%, #232a36, #0b0e13 75%);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- thanh tiêu đề ---------- */
.top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 4px;
}
.back {
  color: var(--muted);
  text-decoration: none;
  font-weight: 700;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  white-space: nowrap;
}
.back:hover {
  color: var(--text);
}
.top h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 19px;
  font-weight: 900;
  letter-spacing: 0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.top h1 span {
  color: var(--gold);
}
.icon-btn {
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.05);
  cursor: pointer;
  font-weight: 800;
  font-size: 16px;
}
.icon-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* ---------- người chơi ---------- */
.hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 2px 12px 6px;
  min-height: 58px;
}
.pl {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 6px 10px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  transition: box-shadow 0.25s, background 0.25s;
}
.pl.right {
  flex-direction: row-reverse;
  text-align: right;
}
.pl.turn {
  background: rgba(255, 211, 77, 0.09);
  box-shadow: 0 0 0 2px var(--gold), 0 0 22px rgba(255, 211, 77, 0.28);
}
.av {
  width: 36px;
  height: 36px;
  flex: none;
  display: grid;
  place-items: center;
  font-size: 21px;
  border-radius: 50%;
  background: var(--panel2);
}
.pmeta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.nm {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.racked {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  min-height: 18px;
}
.pl.right .racked {
  justify-content: flex-end;
}
.racked img {
  width: 17px;
  height: 17px;
  transition: opacity 0.3s;
}
.racked img.gone {
  opacity: 0.16;
  filter: grayscale(1);
}
.racked img.eight {
  margin-left: 4px;
}
.racked img.eight.live {
  filter: drop-shadow(0 0 4px var(--gold));
}
.racked .open {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
.status {
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  max-width: 34vw;
}
.practice {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.is-practice .hud {
  grid-template-columns: minmax(0, 1fr) auto;
}
.is-practice .hud .pl {
  display: none;
}
.is-practice .hud .status {
  text-align: left;
  max-width: none;
}
.is-practice .practice {
  grid-column: 2;
  grid-row: 1;
}
.pstat {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
  margin-left: 4px;
}

/* ---------- nút ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--panel2);
  border-radius: 12px;
  padding: 9px 14px;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover {
  filter: brightness(1.15);
}
.btn.primary {
  background: linear-gradient(#ffcf45, #f2a51c);
  color: #2a1a00;
  border-color: transparent;
}
.btn.ghost {
  background: rgba(255, 255, 255, 0.05);
  padding: 7px 11px;
  font-size: 13px;
}
.btn.ghost.on {
  background: rgba(255, 211, 77, 0.2);
  border-color: var(--gold);
}
.btn.big {
  width: 100%;
  padding: 14px;
  font-size: 17px;
  margin-top: 6px;
}

/* ---------- sân khấu ---------- */
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 70px minmax(0, 1fr) 70px;
  grid-template-areas: "power table side";
  gap: 6px;
  padding: 0 8px 8px;
}
.table-wrap {
  grid-area: table;
  position: relative;
  min-width: 0;
  min-height: 0;
}
#cv {
  position: absolute;
  inset: 0;
  touch-action: none;
  cursor: crosshair;
}
.ctl {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  user-select: none;
  touch-action: none;
}
.lbl {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.power {
  grid-area: power;
  cursor: grab;
}
.track {
  --len: min(66%, 320px);
  position: relative;
  width: 30px;
  height: var(--len);
  border-radius: 16px;
  background: linear-gradient(90deg, #0e1218, #1d2530 50%, #0e1218);
  border: 1px solid var(--line);
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}
#powerFill {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: calc(var(--p, 0) * 100%);
  background: linear-gradient(#ffe066, #ff9a2c 55%, #e5312b);
  box-shadow: 0 0 14px rgba(255, 150, 40, 0.6);
}
#powerKnob {
  position: absolute;
  left: 3px;
  right: 3px;
  height: 22px;
  top: calc(var(--p, 0) * (100% - 22px));
  border-radius: 10px;
  background: linear-gradient(#fafafa, #c9ced6);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
#powerKnob::after {
  content: "";
  position: absolute;
  inset: 9px 5px;
  border-top: 2px solid rgba(0, 0, 0, 0.25);
  border-bottom: 2px solid rgba(0, 0, 0, 0.25);
}
.side {
  grid-area: side;
}
.spin-btn {
  position: relative;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 0;
  cursor: pointer;
  background: radial-gradient(circle at 35% 30%, #fff, #e8e6df 45%, #a9a69c);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.5);
}
#spinDot,
#spinBigDot {
  position: absolute;
  width: 11px;
  height: 11px;
  margin: -5.5px 0 0 -5.5px;
  border-radius: 50%;
  background: #e3262b;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
  pointer-events: none;
}
.fine {
  --off: 0px;
  width: 30px;
  height: min(40%, 170px);
  border-radius: 14px;
  border: 1px solid var(--line);
  cursor: ns-resize;
  background:
    linear-gradient(rgba(11, 14, 19, 0.85), transparent 30%, transparent 70%, rgba(11, 14, 19, 0.85)),
    repeating-linear-gradient(#cfd5de 0 2px, #4b5563 2px 9px);
  background-position: 0 0, 0 var(--off);
  margin-top: 8px;
}
.toast {
  position: absolute;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 8px 16px;
  border-radius: 999px;
  background: rgba(14, 18, 25, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-weight: 800;
  font-size: 14px;
  text-align: center;
  pointer-events: none;
  z-index: 3;
}
.toast.warn {
  border-color: rgba(255, 93, 93, 0.6);
  color: #ffd0d0;
}
.toast.show {
  animation: toast 2.6s ease both;
}
@keyframes toast {
  0% {
    opacity: 0;
    transform: translate(-50%, -8px);
  }
  8%,
  80% {
    opacity: 1;
    transform: translate(-50%, 0);
  }
  100% {
    opacity: 0;
  }
}

/* ---------- lớp phủ ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(5, 8, 12, 0.62);
  backdrop-filter: blur(5px);
}
.sheet {
  position: relative;
  width: min(430px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 22px;
  border-radius: 22px;
  background: linear-gradient(#1f2733, #151b24);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  animation: pop 0.25s ease;
}
@keyframes pop {
  from {
    transform: scale(0.94);
    opacity: 0;
  }
}
.sheet h2 {
  margin: 4px 0 2px;
  text-align: center;
  font-size: 26px;
  font-weight: 900;
}
.sub {
  margin: 0 0 14px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}
.logo {
  display: flex;
  justify-content: center;
  margin-bottom: 4px;
}
.logo i {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  margin: 0 -6px;
  position: relative;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.5);
}
.logo i::after {
  position: absolute;
  inset: 12px;
  border-radius: 50%;
  background: #fff;
  color: #111;
  font: 900 13px/22px "Be Vietnam Pro", sans-serif;
  text-align: center;
}
.logo .b1 {
  background: radial-gradient(circle at 35% 30%, #ffe27a, #f5be14 50%, #9a7400);
}
.logo .b1::after {
  content: "1";
}
.logo .b8 {
  background: radial-gradient(circle at 35% 30%, #666, #161618 50%, #000);
  z-index: 1;
  transform: translateY(-6px) scale(1.12);
}
.logo .b8::after {
  content: "8";
}
.logo .b9 {
  overflow: hidden;
  background: radial-gradient(circle at 35% 30%, #fff, #e8e6df 50%, #a9a69c);
}
.logo .b9::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 24%;
  bottom: 24%;
  background: linear-gradient(#ffd34d, #e0a800);
}
.logo .b9::after {
  content: "9";
}
.group {
  margin: 12px 0;
}
.group label {
  display: block;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}
.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.3);
}
.seg button {
  border: 0;
  border-radius: 10px;
  padding: 9px 6px;
  font-weight: 800;
  font-size: 14px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.seg button.on {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 3px 10px rgba(43, 134, 208, 0.4);
}
.names {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
}
.names input {
  min-width: 0;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.3);
  color: var(--text);
  font: inherit;
  font-weight: 700;
}
.stats {
  margin-top: 12px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
.row {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 16px;
}
.res-ico {
  font-size: 52px;
  text-align: center;
}
.res-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px 10px;
  padding: 12px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.25);
  font-size: 14px;
}
.res-grid b {
  text-align: center;
}
.res-grid .h {
  color: var(--gold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.res-grid span {
  color: var(--muted);
  font-weight: 600;
}
.reopen {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 10;
}
.rules h3 {
  margin: 16px 0 6px;
  font-size: 15px;
  color: var(--gold);
}
.rules p,
.rules li {
  font-size: 14px;
  line-height: 1.55;
  color: #d5dbe4;
}
.rules ul {
  padding-left: 20px;
  margin: 6px 0;
}
.close {
  position: absolute;
  right: 12px;
  top: 10px;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.07);
  font-size: 22px;
  cursor: pointer;
}
.small {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
}
.spin-sheet h3 {
  text-align: center;
  margin: 0 0 12px;
}
.spin-big {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 0 auto 8px;
  border-radius: 50%;
  cursor: crosshair;
  touch-action: none;
  background:
    radial-gradient(circle, transparent 39.5%, rgba(0, 0, 0, 0.12) 40%, transparent 41%),
    radial-gradient(circle at 35% 30%, #fff, #ece9e1 45%, #9d9a90);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
}
#spinBigDot {
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
}
.spin-big span {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  font-weight: 800;
  color: rgba(0, 0, 0, 0.35);
  text-transform: uppercase;
  pointer-events: none;
}
.spin-big .t {
  top: 8px;
}
.spin-big .b {
  bottom: 8px;
}

/* ---------- màn hình dọc: bàn đứng, điều khiển ở dưới ---------- */
@media (orientation: portrait) {
  .stage {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "table table" "power side";
    gap: 8px;
  }
  .power {
    flex-direction: row;
    padding: 0 4px;
  }
  .track {
    width: auto;
    flex: 1;
    height: 34px;
  }
  #powerFill {
    top: 0;
    bottom: 0;
    left: 0;
    right: auto;
    height: auto;
    width: calc(var(--p, 0) * 100%);
    background: linear-gradient(90deg, #ffe066, #ff9a2c 55%, #e5312b);
  }
  #powerKnob {
    top: 3px;
    bottom: 3px;
    height: auto;
    width: 24px;
    left: calc(var(--p, 0) * (100% - 24px));
    right: auto;
  }
  #powerKnob::after {
    inset: 5px 9px;
    border: 0;
    border-left: 2px solid rgba(0, 0, 0, 0.25);
    border-right: 2px solid rgba(0, 0, 0, 0.25);
  }
  .power .lbl {
    min-width: 36px;
    text-align: center;
  }
  .side {
    flex-direction: row;
    gap: 8px;
  }
  .side .lbl {
    display: none;
  }
  .spin-btn {
    width: 46px;
    height: 46px;
  }
  .fine {
    width: 110px;
    height: 34px;
    margin: 0;
    cursor: ew-resize;
    background:
      linear-gradient(90deg, rgba(11, 14, 19, 0.85), transparent 30%, transparent 70%, rgba(11, 14, 19, 0.85)),
      repeating-linear-gradient(90deg, #cfd5de 0 2px, #4b5563 2px 9px);
    background-position: 0 0, var(--off) 0;
  }
  .hud {
    gap: 6px;
    padding: 2px 8px 6px;
  }
  .pl {
    padding: 5px 7px;
    gap: 6px;
  }
  .av {
    width: 30px;
    height: 30px;
    font-size: 18px;
  }
  .racked img {
    width: 14px;
    height: 14px;
  }
  .status {
    font-size: 12px;
    max-width: 26vw;
  }
}
@media (orientation: portrait) and (max-width: 420px) {
  .hud {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
  .hud .status {
    grid-column: 1 / -1;
    grid-row: 2;
    max-width: none;
  }
  .is-practice .hud {
    grid-template-columns: minmax(0, 1fr);
  }
  .is-practice .practice {
    grid-column: 1;
    grid-row: 1;
  }
  .is-practice .hud .status {
    grid-row: 2;
    text-align: center;
  }
  .top h1 span {
    display: none;
  }
}

/* ---------- điện thoại xoay ngang: gộp tiêu đề và thanh người chơi ---------- */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
  }
  .top {
    padding: 6px 8px;
  }
  .top h1 {
    display: none;
  }
  .hud {
    min-height: 0;
    padding: 4px 8px;
  }
  .pl {
    padding: 3px 8px;
  }
  .av {
    width: 28px;
    height: 28px;
    font-size: 16px;
  }
  .racked img {
    width: 13px;
    height: 13px;
  }
  .racked {
    flex-wrap: nowrap;
  }
  .stage {
    grid-column: 1 / -1;
    grid-template-columns: 56px minmax(0, 1fr) 56px;
    padding-bottom: 4px;
  }
  .spin-btn {
    width: 42px;
    height: 42px;
  }
  .lbl {
    font-size: 10px;
  }
}
