:root {
  --bg: #14102e;
  --bg-grad: radial-gradient(120% 90% at 50% 0%, #2a2260 0%, #14102e 55%, #0d0b1f 100%);
  --surface: #241e52;
  --surface-2: #2e2770;
  --ink: #f5f2ff;
  --muted: #b9b0e6;
  --accent: #ec4899;
  --accent-2: #8b5cf6;
  --good: #34d399;
  --bad: #f87171;
  --ring: rgba(255, 255, 255, 0.14);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100dvh;
  background: var(--bg-grad);
  background-color: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  max-width: 480px;
  margin: 0 auto;
  user-select: none;
  -webkit-user-select: none;
}

button, input {
  font: inherit;
  color: inherit;
}

button {
  touch-action: manipulation;
  border: none;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.screen {
  min-height: 100dvh;
}

/* ---------- passcode ---------- */

#passcode-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.passcode-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--ring);
  border-radius: 24px;
  padding: 32px 24px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.passcode-card h1 {
  margin: 0 0 6px;
  font-size: 1.7rem;
}

.passcode-sub {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 0.95rem;
}

.passcode-card input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--ring);
  background: var(--surface-2);
  color: var(--ink);
  font-size: 1.05rem;
  text-align: center;
  letter-spacing: 0.2em;
  outline: none;
}

.passcode-card input:focus {
  border-color: var(--accent);
}

.passcode-card button {
  width: 100%;
  margin-top: 12px;
  padding: 14px;
  border-radius: 14px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
}

.error {
  color: var(--bad);
  font-size: 0.9rem;
  margin: 10px 0 0;
}

/* ---------- header ---------- */

header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(180deg, var(--bg) 0%, rgba(20, 16, 46, 0.85) 80%, transparent 100%);
  backdrop-filter: blur(6px);
}

.brand {
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
}

.tabs {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--ring);
  border-radius: 999px;
  padding: 3px;
}

.tab {
  padding: 8px 16px;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.tab.on {
  background: var(--surface-2);
  color: var(--ink);
}

/* ---------- progress ---------- */

.progress {
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
  margin: 14px 0 10px;
}

/* ---------- card ---------- */

#card-wrap {
  display: flex;
  justify-content: center;
  padding: 4px 16px 0;
}

#card {
  width: min(92vw, 400px);
  aspect-ratio: 1;
  perspective: 1400px;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.65s cubic-bezier(0.4, 0.1, 0.2, 1);
}

#card.flipped .card-inner {
  transform: rotateY(180deg);
}

.face {
  position: absolute;
  inset: 0;
  border-radius: 24px;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
}

.face.front {
  background: var(--surface);
}

.face.back {
  transform: rotateY(180deg);
  display: flex;
  flex-direction: column;
  padding: 22px;
}

#art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scrim {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(10, 8, 30, 0) 45%, rgba(10, 8, 30, 0.82) 100%);
}

.play-btn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 88px;
  height: 88px;
  border-radius: 50%;
  background: rgba(245, 242, 255, 0.94);
  color: #221b4d;
  font-size: 2rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  padding-left: 6px;
}

.meta {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 46px 18px 16px;
}

.meta h2 {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.15;
}

.meta p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.meta #song-desc {
  font-size: 0.85rem;
  margin-top: 6px;
}

.back-label {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.avg-score {
  margin: 0 0 14px;
  font-size: 2.4rem;
  font-weight: 800;
}

.avg-score small {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--muted);
}

.avg-bars {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.bar-row {
  display: grid;
  grid-template-columns: 34px 1fr 40px;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.bar-track {
  height: 10px;
  border-radius: 6px;
  background: var(--surface-2);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

.bar-count {
  text-align: right;
  color: var(--muted);
}

.avg-tags, .avg-hook {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 6px 0 0;
}

.next-btn {
  margin-top: auto;
  padding: 14px;
  border-radius: 14px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
}

/* ---------- rate button ---------- */

.rate-btn {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  max-width: 448px;
  margin: 0 auto;
  padding: 16px;
  border-radius: 18px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 1.1rem;
  font-weight: 800;
  box-shadow: 0 12px 30px rgba(236, 72, 153, 0.35);
  z-index: 30;
}

/* ---------- all rated ---------- */

.all-rated {
  text-align: center;
  padding: 60px 24px;
}

.big-emoji {
  font-size: 3.5rem;
  margin: 0;
}

.all-rated h2 {
  margin: 10px 0 6px;
}

.all-rated p {
  color: var(--muted);
}

/* ---------- bottom sheet ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 6, 22, 0.6);
  z-index: 40;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 480px;
  margin: 0 auto;
  z-index: 50;
  transform: translateY(105%);
  transition: transform 0.3s cubic-bezier(0.3, 0.8, 0.3, 1);
}

.sheet.open {
  transform: translateY(0);
}

.sheet-card {
  background: var(--surface);
  border-radius: 26px 26px 0 0;
  padding: 20px 20px calc(24px + env(safe-area-inset-bottom));
  max-height: 82dvh;
  overflow-y: auto;
  border-top: 1px solid var(--ring);
}

.sheet-close {
  position: absolute;
  top: 12px;
  right: 14px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
}

.sheet-card h3 {
  margin: 0 0 14px;
  padding-right: 40px;
  font-size: 1.2rem;
}

.sheet-hint {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 14px 0 10px;
}

.impressions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.imp-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 18px 8px 14px;
  border-radius: 18px;
  background: var(--surface-2);
  border: 2px solid transparent;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.imp-btn:active {
  transform: scale(0.95);
}

.imp-btn.on {
  border-color: var(--accent);
  background: #3a2f74;
}

.imp-emoji {
  font-size: 2.4rem;
  line-height: 1;
}

.imp-label {
  font-weight: 700;
  font-size: 0.95rem;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-chip {
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.92rem;
  border: 2px solid transparent;
}

.tag-chip.on {
  background: #3a2f74;
  color: var(--ink);
  border-color: var(--accent-2);
}

.hook {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.hook-btn {
  padding: 12px 6px;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  border: 2px solid transparent;
}

.hook-btn.on {
  background: #3a2f74;
  color: var(--ink);
  border-color: var(--accent);
}

.comment {
  width: 100%;
  margin-top: 16px;
  padding: 13px 14px;
  border-radius: 14px;
  border: 1px solid var(--ring);
  background: var(--surface-2);
  color: var(--ink);
  outline: none;
}

.comment:focus {
  border-color: var(--accent);
}

.submit-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 18px;
}

.chosen {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.chosen-chip {
  padding: 10px 14px;
  border-radius: 999px;
  background: #3a2f74;
  font-weight: 700;
  font-size: 0.95rem;
}

.chosen button {
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: underline;
}

.submit-btn {
  flex: 1;
  padding: 15px;
  border-radius: 14px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
}

.submit-btn:disabled {
  opacity: 0.45;
}

/* ---------- battle ---------- */

#battle-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 0 16px;
}

.battle-card {
  position: relative;
  aspect-ratio: 1;
  border-radius: 20px;
  overflow: hidden;
  background: var(--surface);
  padding: 0;
  text-align: left;
}

.battle-card img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.battle-meta {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 34px 10px 10px;
  background: linear-gradient(180deg, transparent, rgba(10, 8, 30, 0.85));
}

.battle-title {
  display: block;
  font-weight: 700;
  font-size: 0.92rem;
  line-height: 1.2;
}

.battle-dur {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 2px;
}

.battle-card.won {
  outline: 4px solid var(--good);
}

.battle-card.lost {
  opacity: 0.45;
}

.battle-result {
  text-align: center;
  font-weight: 700;
  color: var(--good);
  min-height: 24px;
  margin: 12px 0 4px;
}

#leaderboard {
  padding: 8px 16px 40px;
}

.lb-title {
  font-size: 1.05rem;
  margin: 16px 0 10px;
}

.elo-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.elo-row {
  display: grid;
  grid-template-columns: 30px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  background: var(--surface);
  border: 1px solid var(--ring);
  border-radius: 12px;
  margin-bottom: 8px;
}

.elo-rank {
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.elo-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.elo-score {
  font-weight: 800;
  color: var(--accent);
}

.elo-empty {
  color: var(--muted);
  text-align: center;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 70;
  background: #322a6b;
  border: 1px solid var(--ring);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  max-width: 90vw;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.noscript {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}

#player {
  display: none;
}

@media (min-width: 520px) {
  .rate-btn {
    left: 50%;
    transform: translateX(-50%);
    width: 448px;
  }
}
