:root {
  --panel: rgba(18, 24, 38, 0.86);
  --text: #f5f7fb;
  --muted: #a9b3c7;
  --accent: #ffb020;
  --accent-text: #1b1300;
  --danger: #ff5a5a;
  --radius: 18px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #9fd8ff;
  color: var(--text);
}

[hidden] { display: none !important; }

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

button {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 12px;
  padding: 12px 18px;
  background: rgba(255, 255, 255, 0.14);
  color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.24); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); font-size: 1.15rem; }
button.primary:hover:not(:disabled) { background: #ffc34d; }

.panel {
  position: fixed;
  inset: 24px;
  display: flex;
  gap: 24px;
  align-items: stretch;
  justify-content: center;
  pointer-events: none;
}
.panel > * {
  pointer-events: auto;
  background: var(--panel);
  backdrop-filter: blur(6px);
  border-radius: var(--radius);
  padding: 24px 28px;
}

/* Phòng chờ */
#lobby .join {
  width: min(420px, 40vw);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: auto;
}
#lobby h1 { margin: 0; font-size: 2.2rem; }
.sub { margin: 4px 0 14px; color: var(--muted); }
#qr {
  width: min(300px, 32vw);
  aspect-ratio: 1;
  background: #fff;
  border-radius: 14px;
  padding: 10px;
}
.code { margin-top: 14px; font-size: 1.3rem; }
.code b { font-size: 2rem; letter-spacing: 0.2em; color: var(--accent); }
.url { margin-top: 6px; color: var(--muted); font-size: 0.9rem; word-break: break-all; }
.warn { margin: 10px 0 0; color: #ffd166; font-size: 0.85rem; }

#lobby .players {
  width: min(480px, 44vw);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#lobby h2 { margin: 0 0 12px; }
#playerCount {
  display: inline-block;
  min-width: 2em;
  padding: 0 8px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.15);
  text-align: center;
}
/* Độ khó */
.difficulty { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 14px; }
.difficulty .label { color: var(--muted); margin-right: 4px; }
.difficulty button { padding: 8px 12px; font-size: 0.95rem; border: 2px solid transparent; }
.difficulty button.sel { border-color: var(--accent); background: rgba(255, 176, 32, 0.18); }
.difficulty-desc { margin: 6px 0 0; color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }
#results .difficulty { justify-content: center; }

/* Gói miễn phí / Pro */
.tier { margin-bottom: 12px; }
.tier-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.badge {
  padding: 4px 12px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.12);
  font-weight: 700;
  font-size: 0.9rem;
}
.badge.pro { background: linear-gradient(90deg, #ffb020, #ff7a3d); color: #1b1300; }
button.link {
  padding: 2px 4px;
  background: transparent;
  color: var(--accent);
  text-decoration: underline;
  font-size: 0.9rem;
}
button.link:hover:not(:disabled) { background: transparent; color: #ffc34d; }
.license-form { display: flex; gap: 8px; margin-top: 10px; }
.license-form input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font: inherit;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.license-form button { padding: 10px 14px; font-size: 1rem; }
.license-msg { margin: 8px 0 0; font-size: 0.9rem; color: #ffd166; }
.license-msg.ok { color: #7ee08a; }

#playerList {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-content: start;
}
#playerList li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-left: 6px solid var(--c);
  min-width: 0;
}
#playerList li.off { opacity: 0.45; }
#playerList .emoji { font-size: 1.6rem; }
#playerList .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
#playerList .kick { padding: 2px 8px; background: transparent; color: var(--muted); }
#playerList .kick:hover { color: var(--danger); }
.hint { color: var(--muted); }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.buttons.small button { font-size: 0.85rem; padding: 8px 12px; }

/* Trong lúc đua */
#hud { position: fixed; inset: 0; pointer-events: none; }
#standings {
  position: absolute;
  top: 16px;
  left: 16px;
  margin: 0;
  padding: 10px 12px;
  list-style: none;
  background: var(--panel);
  border-radius: 14px;
  min-width: 200px;
  max-height: calc(100% - 38vh - 60px); /* chừa chỗ cho thanh tiến độ ở góc dưới trái */
  overflow: hidden;
}
#standings li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  font-weight: 600;
}
#standings .pos { width: 1.6em; text-align: right; color: var(--muted); }
#standings .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--c); flex: none; }
#standings .fin { margin-left: auto; color: var(--accent); }

/* Thanh tiến độ dọc: góc dưới bên trái */
#progress {
  position: absolute;
  left: 34px;
  bottom: 24px;
  height: 32vh;
  width: 10px;
  border-radius: 99px;
  background: rgba(0, 0, 0, 0.3);
}
#progress .finish { position: absolute; top: -40px; left: 50%; transform: translateX(-50%); font-size: 1.6rem; }
#progress .marker {
  position: absolute;
  left: 50%;
  width: 22px;
  height: 22px;
  margin-top: -11px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: var(--c);
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

/* Bản đồ nhỏ: góc dưới bên phải */
#minimap {
  position: absolute;
  right: 16px;
  bottom: 16px;
  height: 42vh;
  width: calc(42vh * 0.42);
  min-width: 110px;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

#countdown {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: min(30vw, 40vh);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}
#countdown.go { color: var(--accent); }

#toasts {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  padding: 8px 16px;
  border-radius: 99px;
  background: var(--panel);
  font-weight: 600;
  animation: toast 2.4s forwards;
}
@keyframes toast {
  0% { opacity: 0; transform: translateY(10px); }
  10%, 80% { opacity: 1; transform: none; }
  100% { opacity: 0; }
}

/* Kết quả */
#results .results-inner { width: min(560px, 90vw); overflow: auto; }
#results h1 { margin-top: 0; text-align: center; }
#resultList { list-style: none; margin: 0; padding: 0; }
#resultList li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  margin-bottom: 6px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-left: 6px solid var(--c);
  font-size: 1.15rem;
}
#resultList .medal { font-size: 1.8rem; width: 1.6em; text-align: center; }
#resultList .emoji { font-size: 1.6rem; }
#resultList .name { flex: 1; font-weight: 700; }
#resultList .time { color: var(--muted); font-variant-numeric: tabular-nums; }
#results .buttons { justify-content: center; }

#loading {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: #9fd8ff;
  color: #123;
  font-size: 1.5rem;
  font-weight: 700;
}

@media (max-width: 800px) {
  .panel { flex-direction: column; inset: 12px; overflow: auto; }
  #lobby .join, #lobby .players { width: auto; }
  #qr { width: min(260px, 60vw); }
  #playerList { grid-template-columns: 1fr; }
}
