:root {
  --ink: #2b2140;
  --muted: #6d5a7a;
  --paper: #fffdf8;
  --pink: #ff8fb8;
  --mint: #7ce7c4;
  --lemon: #ffe066;
  --lav: #d4b3ff;
  --sky: #8ec5ff;
}

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

html, body, #app { height: 100%; }

body {
  font-family: "Gowun Dodum", sans-serif;
  color: var(--paper);
  background: #1a1230;
  overflow: hidden;
}

#app { position: relative; }

#view {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.back, .icon-btn, button, .diff-row button, .song-card {
  font-family: "Jua", sans-serif;
}

.back {
  position: absolute;
  z-index: 5;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  color: var(--ink);
  background: rgba(255, 253, 248, 0.92);
  text-decoration: none;
  border-radius: 999px;
  padding: 8px 14px;
  box-shadow: 0 8px 24px rgba(20, 10, 40, 0.18);
}

.icon-btn, #btn-quit, .actions button, .diff-row button {
  border: 0;
  cursor: pointer;
  border-radius: 999px;
}

.icon-btn {
  position: absolute;
  z-index: 5;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  background: rgba(255, 253, 248, 0.92);
  color: var(--ink);
  font-size: 18px;
}

#select, #result {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: auto;
  padding: max(64px, calc(env(safe-area-inset-top) + 52px)) 20px 32px;
}

#select {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.kicker {
  letter-spacing: 0.22em;
  font-size: 12px;
  color: #ffe9a8;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

h1, h2 { font-family: "Jua", sans-serif; font-weight: 400; }

h1 {
  font-size: clamp(46px, 8vw, 78px);
  line-height: 1;
  text-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

.lead {
  max-width: 440px;
  text-align: center;
  margin: 8px 0 18px;
  color: rgba(255, 253, 248, 0.88);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

.diff-row { display: flex; gap: 8px; margin-bottom: 16px; }

.diff-row button {
  min-width: 92px;
  min-height: 42px;
  padding: 0 16px;
  background: rgba(255, 253, 248, 0.16);
  color: white;
  backdrop-filter: blur(8px);
}

.diff-row button.on {
  background: white;
  color: var(--ink);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
}

.song-row {
  width: min(960px, 100%);
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.song-card {
  text-align: left;
  border: 0;
  border-radius: 28px;
  overflow: hidden;
  background: rgba(255, 253, 248, 0.96);
  color: var(--ink);
  box-shadow: 0 18px 40px rgba(20, 8, 40, 0.28);
  cursor: pointer;
  display: flex;
  flex-direction: column;
}

.song-card:active { transform: translateY(2px); }

.art-canvas {
  width: 100%;
  height: 188px;
  display: block;
  background: #241833;
}

.card-body { padding: 14px 16px 16px; }

.card-body h2 { font-size: 26px; }

.bpm {
  display: inline-block;
  margin-top: 4px;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.card-body p { margin-top: 6px; color: #4d3d62; }

.hi { margin-top: 8px; font-family: "Jua", sans-serif; color: #c45b86; }

.offset {
  margin-top: 18px;
  width: min(520px, 100%);
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  background: rgba(255, 253, 248, 0.14);
  border-radius: 16px;
  padding: 10px 14px;
}

.offset input { width: 100%; accent-color: var(--lemon); }

.offset-help, .help {
  width: min(560px, 100%);
  margin-top: 8px;
  font-size: 13px;
  color: rgba(255, 253, 248, 0.8);
  text-align: center;
  list-style: none;
}

.help kbd {
  display: inline-block;
  min-width: 1.4em;
  padding: 1px 6px;
  margin: 0 2px;
  border-radius: 6px;
  background: white;
  color: var(--ink);
}

#hud {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.hud-top, .hud-stats {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

.hud-top { top: 0; left: 0; right: 0; height: 0; pointer-events: none; }
#now-playing {
  display: block;
  position: absolute;
  top: 54px;
  left: 16px;
  right: 16px;
  margin: 0;
  text-align: center;
  font-family: "Jua", sans-serif;
  font-size: 18px;
  line-height: 1.1;
  color: #fffdf8;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.55);
  pointer-events: none;
}
#btn-quit {
  position: absolute;
  z-index: 6;
  pointer-events: auto;
  top: max(12px, env(safe-area-inset-top));
  right: calc(max(12px, env(safe-area-inset-right)) + 52px);
  background: rgba(255, 253, 248, 0.94);
  color: var(--ink);
  min-height: 36px;
  padding: 0 14px;
  box-shadow: 0 8px 20px rgba(20, 10, 40, 0.18);
}

.hud-stats, .combo-wrap, .judge-pop, .mile { display: none !important; }

.hud-stats {
  top: max(96px, calc(env(safe-area-inset-top) + 86px));
  font-size: 14px;
  gap: 18px;
  justify-content: flex-start;
}
.hud-stats b { font-family: "Jua", sans-serif; font-size: 22px; margin-left: 6px; }

.combo-wrap {
  position: absolute;
  left: max(20px, env(safe-area-inset-left));
  bottom: max(24px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  line-height: 0.85;
  opacity: 0;
  transform: scale(0.9);
  transition: opacity 0.15s ease;
}
.combo-wrap.on { opacity: 1; }
.combo-wrap b {
  font-family: "Jua", sans-serif;
  font-size: clamp(48px, 8vw, 84px);
  text-shadow: 0 8px 0 rgba(43, 33, 64, 0.25), 0 0 24px rgba(255, 224, 102, 0.45);
}
.combo-wrap small { letter-spacing: 0.18em; font-size: 12px; }

.judge-pop {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, 8px);
  font-family: "Jua", sans-serif;
  font-size: clamp(28px, 5vw, 46px);
  opacity: 0;
  text-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
}
.judge-pop.show { animation: pop 1s ease forwards; }
.judge-pop.perfect { color: #fff6c8; }
.judge-pop.great { color: #b8ffe8; }
.judge-pop.good { color: #d4b3ff; }
.judge-pop.miss { color: #ffb4c8; }

@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, 18px) scale(0.8); }
  30% { opacity: 1; transform: translate(-50%, 0) scale(1.06); }
  100% { opacity: 0; transform: translate(-50%, -16px) scale(1); }
}

.mile {
  position: absolute;
  left: 50%;
  top: 30%;
  transform: translate(-50%, 0);
  font-family: "Jua", sans-serif;
  font-size: 22px;
  letter-spacing: 0.08em;
  color: white;
  background: linear-gradient(90deg, transparent, rgba(43, 33, 64, 0.55), transparent);
  padding: 8px 28px;
  opacity: 0;
}
.mile.show { animation: pop 0.8s ease; }

.touch-lanes {
  position: absolute;
  z-index: 4;
  display: none;
  pointer-events: none;
  grid-template-columns: repeat(4, 1fr);
  touch-action: none;
}

.touch-lanes.is-on {
  display: grid;
  pointer-events: auto;
}

.touch-lanes[hidden] {
  display: none !important;
  pointer-events: none !important;
}

.start-error {
  width: min(560px, 100%);
  margin: 0 0 14px;
  padding: 10px 14px;
  border-radius: 14px;
  background: #fff1f4;
  color: #6e1834;
  font-weight: 700;
  text-align: center;
  box-shadow: 0 8px 24px rgba(20, 8, 40, 0.18);
}

.start-error[hidden] { display: none !important; }

.touch-lanes button {
  border: 0;
  background: transparent;
  color: transparent;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

#result { display: grid; place-items: center; background: rgba(18, 10, 32, 0.28); }

.sheet {
  width: min(460px, calc(100% - 24px));
  background: rgba(255, 253, 248, 0.96);
  color: var(--ink);
  border-radius: 32px;
  padding: 28px 22px 22px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(10, 6, 24, 0.35);
}

.sheet .kicker { color: #a48bb8; text-shadow: none; }
.sheet .lead {
  color: #2b2140;
  text-shadow: none;
  font-size: 18px;
  font-weight: 700;
  margin: 4px 0 10px;
}
.sheet h2 { color: var(--ink); }

.party {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}
.party i {
  position: absolute;
  top: -12%;
  width: 10px;
  height: 16px;
  border-radius: 2px;
  animation: fall 1.15s ease-in forwards;
}
@keyframes fall {
  to { transform: translateY(105vh) rotate(240deg); opacity: 0.15; }
}

.rank {
  width: 120px;
  height: 120px;
  margin: 10px auto 6px;
  border-radius: 36px;
  display: grid;
  place-items: center;
  font-family: "Jua", sans-serif;
  font-size: 72px;
  color: var(--ink);
  background: linear-gradient(160deg, #fff6c8, #ff8fb8);
  box-shadow: inset 0 -8px 0 rgba(43, 33, 64, 0.08), 0 12px 30px rgba(255, 143, 184, 0.35);
}
.rank.S { background: linear-gradient(160deg, #fff6c8, #ffe066); }
.rank.A { background: linear-gradient(160deg, #ffe0ee, #ff8fb8); }
.rank.B { background: linear-gradient(160deg, #e7fff6, #7ce7c4); }
.rank.C { background: linear-gradient(160deg, #e7f2ff, #8ec5ff); }
.rank.D { background: linear-gradient(160deg, #eee, #c9bdd4); }
.rank.reveal { animation: rankIn 0.85s cubic-bezier(0.16, 1.25, 0.32, 1) both; }
@keyframes rankIn {
  0% { transform: scale(0.12) rotate(-16deg); filter: blur(8px); opacity: 0; }
  58% { transform: scale(1.16) rotate(5deg); filter: none; opacity: 1; }
  100% { transform: none; opacity: 1; }
}

.score-xl { margin: 8px 0; }
.score-xl span { letter-spacing: 0.14em; font-size: 12px; color: var(--muted); }
.score-xl b { display: block; font-family: "Jua", sans-serif; font-size: 40px; }

.stats {
  list-style: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 8px 0 16px;
}
.stats li {
  background: #fff6fb;
  border-radius: 14px;
  padding: 8px;
}
.stats b { font-family: "Jua", sans-serif; display: block; font-size: 20px; }

.actions { display: flex; gap: 8px; justify-content: center; }
.actions button {
  min-height: 46px;
  padding: 0 18px;
  background: var(--ink);
  color: white;
}
.actions .ghost { background: #efe6f6; color: var(--ink); }

.hidden { display: none !important; }

.kb-only { display: none; }
.touch-only { display: none; }
body.fine .kb-only { display: list-item; }
body.coarse .touch-only { display: list-item; }
.offset-help { display: block; }

@media (max-width: 800px), (orientation: portrait) {
  .song-row { grid-template-columns: 1fr; max-width: 460px; }
  .art-canvas { height: 168px; }
  .combo-wrap { bottom: auto; top: 46%; }
}

@media (orientation: landscape) and (max-height: 520px) {
  #select { padding-top: 52px; }
  h1 { font-size: 40px; }
  .art-canvas { height: 104px; }
  .song-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .card-body p, .offset-help, .help { display: none; }
}
