:root {
  --bg: #12141b;
  --bg2: #1a1d27;
  --card: #20242f;
  --line: #2e3342;
  --text: #eef0f5;
  --muted: #9aa1b2;
  --accent: #e6473d;
  --accent2: #f2c73c;
  --ok: #3fbf6f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: radial-gradient(1200px 700px at 70% -10%, #232a3f 0%, var(--bg) 55%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}

.screen { display: none; height: 100vh; flex-direction: column; }
.screen.active { display: flex; }

/* ------------------------------------------------------------- bits ----- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
}

.btn {
  background: var(--bg2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.06s, background 0.15s;
}
.btn:hover:not(:disabled) { background: #262b39; }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: transparent; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: #f05a50; }
.btn.big { padding: 13px 22px; font-size: 16px; width: 100%; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.active { outline: 2px solid var(--accent2); }

input, select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg2);
  color: var(--text);
  font-size: 15px;
}
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
label input, label select { margin-top: 5px; }
label.inline { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
label.inline select { width: auto; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
hr { border: none; border-top: 1px solid var(--line); margin: 18px 0; }
.row-gap { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; align-items: center; }

.toast {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translate(-50%, -80px);
  background: var(--ok);
  color: #0c1a10;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 999px;
  z-index: 50;
  transition: transform 0.25s;
  max-width: 90vw;
}
.toast.show { transform: translate(-50%, 0); }
.toast.error { background: var(--accent); color: #fff; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(18, 20, 27, 0.75);
}
.topbar.slim { padding: 8px 20px; font-size: 14px; }
.logo-small { font-weight: 800; letter-spacing: 0.5px; }
.who { color: var(--accent2); font-weight: 600; }
.code { color: var(--accent2); letter-spacing: 3px; font-size: 18px; }

/* ------------------------------------------------------------- auth ----- */
#screen-auth { align-items: center; justify-content: center; }
.auth-card { width: min(400px, 92vw); text-align: center; }
.logo {
  font-size: 42px;
  line-height: 1.02;
  font-weight: 900;
  letter-spacing: 1px;
  background: linear-gradient(120deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-transform: uppercase;
}
.tagline { color: var(--muted); margin: 10px 0 18px; }
.tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.tabs button {
  flex: 1;
  padding: 9px;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
}
.tabs button.active { color: var(--text); border-color: var(--accent); }
#auth-form { text-align: left; }

/* ------------------------------------------------------------- home ----- */
.home-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
  padding: 20px;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
  overflow-y: auto;
  align-content: start;
}
.me-card { display: flex; flex-direction: column; align-items: center; gap: 12px; }
#home-me { width: 170px; height: 260px; }
.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: 4px; font-weight: 700; }

/* ----------------------------------------------------------- avatar ----- */
.avatar-grid {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
  padding: 20px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
  flex: 1;
}
.avatar-left { display: flex; flex-direction: column; align-items: center; min-height: 0; }
/* Explicit CSS height: the preview loop sizes the backing store from the
   element's rect, so the rect must be layout-fixed - a flex-grown canvas
   feeds its own attribute size back into layout and zooms out of frame. */
#avatar-preview { width: 100%; height: min(52vh, 440px); min-height: 280px; display: block; }
.inst-row { display: flex; gap: 6px; margin-top: 8px; }
.avatar-right { overflow-y: auto; }
.avatar-row {
  display: grid;
  grid-template-columns: 1fr 34px 64px 34px;
  gap: 6px;
  align-items: center;
  padding: 4px 0;
}
.avatar-label { font-size: 13px; color: var(--muted); }
.avatar-val { text-align: center; font-size: 13px; }

/* ------------------------------------------------------------ lobby ----- */
.lobby-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 16px;
  padding: 16px 20px;
  flex: 1;
  overflow: hidden;
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
}
.lobby-grid .card { overflow-y: auto; }
h2 { font-size: 15px; margin: 14px 0 8px; }
h2:first-child { margin-top: 0; }

.seats { display: flex; gap: 10px; flex-wrap: wrap; }
.seat {
  width: 108px;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  text-align: center;
}
.seat.ready { border-color: var(--ok); }
.seat-avatar { width: 76px; height: 110px; }
.seat-name { font-size: 12px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seat-inst { font-size: 11px; color: var(--muted); }
.seat-status { font-size: 10px; color: var(--ok); font-weight: 800; height: 12px; }

.inst-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.btn.inst { display: flex; flex-direction: column; gap: 3px; padding: 10px 6px; }
.btn.inst small { color: var(--muted); font-size: 10px; }

.song-list { display: flex; flex-direction: column; gap: 6px; }
.song {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 12px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}
.song.picked { border-color: var(--accent2); background: #262536; }
.song:disabled { cursor: default; opacity: 0.8; }
.song-title { font-weight: 700; font-size: 14px; }
.song-meta { font-size: 11px; color: var(--muted); white-space: nowrap; }

.chat-log {
  height: 130px;
  overflow-y: auto;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 13px;
  margin-bottom: 8px;
}
.chat-line { padding: 1px 0; }
.chat-line.system { color: var(--muted); font-style: italic; }

/* ------------------------------------------------------------- play ----- */
#screen-play { background: #0d0f15; }
.play-grid {
  flex: 1;
  display: grid;
  grid-template-rows: 28% 1fr;
  overflow: hidden;
}
#stage { width: 100%; height: 100%; }
#highway { width: 100%; height: 100%; touch-action: none; }

/* ---------------------------------------------------------- results ----- */
#screen-results { align-items: center; justify-content: center; }
.results-card { width: min(460px, 94vw); text-align: center; }
.stars { font-size: 44px; color: var(--accent2); letter-spacing: 6px; }
.band-score { font-size: 18px; margin: 10px 0 16px; }
.result-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  align-items: baseline;
}
.result-row .result-score { font-weight: 800; color: var(--accent2); }
#btn-back-lobby { margin-top: 18px; }

/* ------------------------------------------------- history + levels ----- */
.level { font-weight: 800; color: var(--accent2); }
.history-card { grid-column: 1 / -1; }
.inst-bests { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-bottom: 10px; }
.inst-best { display: flex; flex-direction: column; }
.inst-best > span:first-child { font-weight: 700; font-size: 13px; text-transform: capitalize; }
.history-list { display: flex; flex-direction: column; }
.history-row {
  display: grid;
  grid-template-columns: 52px 1fr auto auto 84px;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  align-items: baseline;
}
.history-row .history-score { text-align: right; font-weight: 800; color: var(--accent2); }
.dim { opacity: 0.35; }

/* ------------------------------------------------------------ venue ----- */
.venue-grid {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 16px;
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
  overflow: hidden;
}
#venue-map { height: 340px; margin-top: 10px; border-radius: 10px; }
.minimap {
  position: relative;
  overflow: hidden;
  background: #0d0f15;
  cursor: crosshair;
  user-select: none;
  touch-action: none;
}
.minimap .mm-tiles { position: absolute; inset: 0; }
.minimap .mm-tiles img { position: absolute; filter: saturate(0.8) brightness(0.9); }
.minimap .mm-pin {
  position: absolute;
  transform: translate(-50%, -95%);
  font-size: 26px;
  pointer-events: none;
  text-shadow: 0 2px 4px rgba(0,0,0,0.6);
}
.minimap .mm-zoom {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.minimap .mm-zoom button {
  width: 30px; height: 30px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: rgba(24, 27, 36, 0.9);
  color: var(--text);
  font-size: 16px;
  cursor: pointer;
}
.minimap .mm-attrib {
  position: absolute;
  bottom: 2px;
  right: 6px;
  font-size: 10px;
  color: rgba(255,255,255,0.65);
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}
.minimap .mm-attrib a { color: inherit; }

.venue-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.venue-bar .venue-name { font-weight: 700; font-size: 14px; }
.venue-bar select { width: auto; padding: 4px 8px; font-size: 12px; margin-left: auto; }

/* ----------------------------------------------------------- parody ----- */
.parody-grid {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 16px;
  padding: 20px;
  max-width: 950px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
  overflow-y: auto;
  align-content: start;
}
textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  margin-top: 5px;
}

/* ----------------------------------------------------------- mobile ----- */
@media (max-width: 700px) {
  .home-grid, .avatar-grid, .lobby-grid, .venue-grid, .parody-grid { grid-template-columns: 1fr; overflow-y: auto; }
  .lobby-grid .card { overflow: visible; }
  html, body { overflow: auto; }
  .screen.active { min-height: 100vh; height: auto; }
  #screen-play.active { height: 100vh; }
  .play-grid { grid-template-rows: 22% 1fr; }
}
