:root {
  color-scheme: dark;
  --bg: #101318;
  --panel: #181d25;
  --panel-soft: #202632;
  --line: #343d4c;
  --text: #f4f0e8;
  --muted: #a6acb7;
  --accent: #00c2a8;
  --accent-2: #f2c14e;
  --danger: #ff5d73;
  --blue: #4ea8ff;
  --target-y: 22%;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(0, 194, 168, 0.13), transparent 34rem),
    linear-gradient(145deg, #101318 0%, #17141b 54%, #101318 100%);
  color: var(--text);
}

body.is-immersive {
  overflow: hidden;
}

body.is-performing {
  overflow: hidden;
}

button,
select,
input {
  font: inherit;
}

.game-shell {
  width: min(1180px, calc(100vw - 28px));
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: 22px 0 28px;
  position: relative;
}

body.is-immersive .game-shell,
:fullscreen .game-shell,
:-webkit-full-screen .game-shell {
  width: min(100vw - 18px, 1220px);
  min-height: 100svh;
  padding: 10px 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

body.is-performing .game-shell {
  width: min(100vw - 12px, 1220px);
  height: 100vh;
  height: 100svh;
  min-height: 0;
  padding: 8px 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
}

body.is-performing .brand-block,
body.is-performing .song-picker,
body.is-performing #startBtn,
body.is-performing #demoBtn,
body.is-performing .score-strip {
  display: none;
}

body.is-performing .topbar {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  z-index: 30;
  display: block;
}

body.is-performing .controls {
  display: flex;
  gap: 6px;
}

body.is-performing .icon-btn {
  width: 38px;
  min-height: 38px;
  border-color: rgba(255, 255, 255, 0.2);
  background: rgba(24, 29, 37, 0.52);
  backdrop-filter: blur(8px);
}

body.is-performing .stage-wrap {
  height: auto;
  min-height: 0;
}

body.is-performing .keyboard {
  height: clamp(78px, 15svh, 124px);
}

.topbar,
.score-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.brand-block h1,
.result-content h2 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 3.6rem);
  line-height: 1;
  letter-spacing: 0;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent-2);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
}

.song-picker {
  position: relative;
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.song-menu-button,
button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel);
}

.song-menu-button {
  width: min(48vw, 230px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  text-align: left;
}

button {
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.34);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
  transform: none;
}

.native-select {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.menu-caret {
  color: var(--accent-2);
  font-size: 0.9rem;
}

.song-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  z-index: 20;
  width: min(64vw, 260px);
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: #181d25;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.38);
}

.song-option {
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 0;
  padding: 0 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-size: 0.92rem;
  font-weight: 800;
}

.song-option:hover,
.song-option.active {
  background: rgba(0, 194, 168, 0.14);
}

.primary-btn {
  min-width: 96px;
  padding: 0 18px;
  border-color: rgba(0, 194, 168, 0.35);
  background: linear-gradient(135deg, #00a891, #2478dd);
  color: white;
  font-weight: 850;
}

.secondary-btn {
  min-width: 86px;
  padding: 0 16px;
  border-color: rgba(242, 193, 78, 0.42);
  background: linear-gradient(135deg, rgba(242, 193, 78, 0.23), rgba(255, 122, 89, 0.18));
  color: var(--text);
  font-weight: 850;
}

.icon-btn {
  width: 44px;
  font-size: 1.35rem;
  line-height: 1;
}

.icon-btn.active {
  border-color: rgba(242, 193, 78, 0.62);
  background: rgba(242, 193, 78, 0.18);
  color: var(--accent-2);
}

.score-strip {
  margin: 18px 0 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(24, 29, 37, 0.86);
}

.score-strip > div {
  display: grid;
  gap: 3px;
  min-width: 72px;
}

.metric-label {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 750;
}

.score-strip strong {
  font-size: clamp(1.05rem, 2.4vw, 1.55rem);
  line-height: 1.1;
}

.best-name {
  max-width: 100%;
  overflow: hidden;
  color: var(--accent-2);
  font-size: 0.74rem;
  font-weight: 850;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mobile-best-record {
  display: none;
}

.live-score {
  display: none;
}

body.is-performing .live-score {
  position: fixed;
  left: max(8px, env(safe-area-inset-left));
  top: max(8px, env(safe-area-inset-top));
  z-index: 29;
  display: grid;
  gap: 2px;
  min-width: 96px;
  padding: 7px 10px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: rgba(24, 29, 37, 0.58);
  backdrop-filter: blur(8px);
}

.live-score span {
  color: var(--muted);
  font-size: 0.64rem;
  font-weight: 850;
}

.live-score strong {
  color: var(--text);
  font-size: 1.08rem;
  line-height: 1;
}

.stage-wrap {
  position: relative;
  height: clamp(360px, 56vh, 610px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(to right, rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(180deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.015)),
    #11161d;
  background-size: calc(100% / 8) 100%, 100% 100%, 100% 100%;
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.28);
}

.stage-wrap::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: var(--target-y);
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--accent-2), white, var(--accent-2), transparent);
  box-shadow: 0 0 18px rgba(242, 193, 78, 0.64);
  pointer-events: none;
}

.target-label {
  position: absolute;
  right: 12px;
  top: calc(var(--target-y) - 24px);
  color: rgba(244, 240, 232, 0.72);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  pointer-events: none;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.keyboard {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 6px;
  height: clamp(92px, 14vh, 132px);
  margin-top: 10px;
}

body.is-immersive .stage-wrap,
:fullscreen .stage-wrap,
:-webkit-full-screen .stage-wrap {
  height: auto;
  min-height: 0;
}

body.is-immersive .keyboard,
:fullscreen .keyboard,
:-webkit-full-screen .keyboard {
  height: clamp(76px, 15svh, 120px);
}

.piano-key {
  position: relative;
  overflow: hidden;
  border: 1px solid #c6ccd5;
  border-radius: 0 0 8px 8px;
  background: linear-gradient(180deg, #fffaf0, #d8dde5);
  color: #1a1d23;
  cursor: pointer;
  user-select: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 5px;
  padding: 0 4px 12px;
  transition: transform 70ms ease, filter 70ms ease, box-shadow 70ms ease;
}

.piano-key:nth-child(2),
.piano-key:nth-child(4),
.piano-key:nth-child(7) {
  background: linear-gradient(180deg, #252b36, #0d1016);
  color: #f6f1e8;
  border-color: #11141a;
}

.piano-key.active {
  transform: translateY(3px);
  filter: brightness(1.12);
  box-shadow: inset 0 0 0 999px rgba(0, 194, 168, 0.22), 0 0 24px rgba(0, 194, 168, 0.28);
}

.key-label {
  font-size: clamp(1.05rem, 2.8vw, 1.55rem);
  font-weight: 950;
  line-height: 1;
}

.note-label {
  color: inherit;
  opacity: 0.68;
  font-size: 0.72rem;
  font-weight: 800;
}

.result-panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(8, 10, 14, 0.64);
  backdrop-filter: blur(8px);
  z-index: 40;
}

.result-panel[hidden] {
  display: none;
}

.result-content {
  width: min(430px, 100%);
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: #181d25;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
}

.result-content p:not(.eyebrow) {
  margin: 14px 0 20px;
  color: var(--muted);
  line-height: 1.65;
}

.record-form {
  display: grid;
  gap: 9px;
  margin: 16px 0 12px;
}

.record-form[hidden] {
  display: none;
}

.record-form label {
  color: var(--accent-2);
  font-size: 0.82rem;
  font-weight: 850;
}

.record-input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.record-input-row input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--text);
  background: rgba(16, 19, 24, 0.9);
  outline: none;
}

.record-input-row input:focus {
  border-color: rgba(242, 193, 78, 0.68);
  box-shadow: 0 0 0 3px rgba(242, 193, 78, 0.14);
}

.best-record-text {
  margin: 8px 0 16px;
  color: var(--accent-2);
  font-weight: 800;
}

.result-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.home-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border-radius: 8px;
  text-decoration: none;
}

@media (max-width: 720px) {
  .game-shell {
    width: calc(100vw - 12px);
    height: 100vh;
    height: 100svh;
    min-height: 0;
    padding: 7px 0 max(7px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
  }

  .brand-block h1 {
    font-size: clamp(1.05rem, 5.8vw, 1.6rem);
  }

  .eyebrow {
    display: none;
  }

  .topbar,
  .score-strip {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar {
    gap: 7px;
  }

  .controls {
    display: grid;
    grid-template-columns: 1fr 1fr 44px 44px;
    gap: 7px;
    justify-content: stretch;
  }

  .song-picker {
    grid-column: 1 / -1;
  }

  .primary-btn,
  .secondary-btn {
    min-width: 0;
    padding: 0 10px;
  }

  .song-menu-button {
    width: 100%;
  }

  .song-picker {
    gap: 3px;
  }

  .song-picker > span {
    font-size: 0.66rem;
  }

  .song-menu-button,
  button {
    min-height: 38px;
  }

  .song-menu {
    width: 100%;
  }

  .score-strip {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
    margin: 7px 0;
    padding: 6px 8px;
    background: rgba(24, 29, 37, 0.62);
  }

  .score-strip > div {
    min-width: 0;
  }

  .score-strip > div:last-child {
    display: none;
  }

  .mobile-best-record {
    display: block;
    max-width: 100%;
    overflow: hidden;
    margin: 0 0 7px;
    padding: 6px 9px;
    border: 1px solid rgba(242, 193, 78, 0.28);
    border-radius: 8px;
    color: var(--accent-2);
    background: rgba(24, 29, 37, 0.58);
    font-size: 0.72rem;
    font-weight: 850;
    line-height: 1.1;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.is-performing .mobile-best-record,
  body.is-immersive .mobile-best-record,
  :fullscreen .mobile-best-record,
  :-webkit-full-screen .mobile-best-record {
    position: fixed;
    left: max(6px, env(safe-area-inset-left));
    top: max(6px, env(safe-area-inset-top));
    z-index: 26;
    max-width: min(44vw, 230px);
    margin: 0;
    background: rgba(24, 29, 37, 0.5);
    backdrop-filter: blur(8px);
  }

  body.is-performing .live-score {
    left: auto;
    right: max(92px, calc(env(safe-area-inset-right) + 92px));
    top: max(6px, env(safe-area-inset-top));
    min-width: 82px;
    padding: 6px 8px;
  }

  .metric-label {
    font-size: 0.66rem;
  }

  .score-strip strong {
    font-size: clamp(0.82rem, 3.6vw, 1.08rem);
  }

  .best-name {
    font-size: 0.62rem;
  }

  .stage-wrap {
    height: auto;
    min-height: 0;
  }

  .keyboard {
    gap: 4px;
    height: clamp(78px, 14svh, 108px);
    margin-top: 7px;
  }

  .piano-key {
    padding-bottom: 9px;
  }

  .key-label {
    font-size: clamp(0.9rem, 4.4vw, 1.2rem);
  }

  .note-label {
    display: none;
  }

  body.is-performing .game-shell,
  body.is-immersive .game-shell,
  :fullscreen .game-shell,
  :-webkit-full-screen .game-shell {
    width: calc(100vw - 8px);
    padding: 4px 0 max(4px, env(safe-area-inset-bottom));
    grid-template-rows: minmax(0, 1fr) auto;
  }

  body.is-performing .keyboard,
  body.is-immersive .keyboard,
  :fullscreen .keyboard,
  :-webkit-full-screen .keyboard {
    height: clamp(86px, 17svh, 126px);
  }
}

@media (max-width: 420px) {
  .game-shell {
    width: calc(100vw - 12px);
    padding-top: 6px;
  }

  .topbar {
    gap: 6px;
  }

  .controls {
    gap: 6px;
    grid-template-columns: 1fr 1fr 40px 40px;
  }

  .song-menu-button,
  button {
    min-height: 40px;
  }

  .icon-btn {
    width: 40px;
    font-size: 1.18rem;
  }

  .stage-wrap {
    height: auto;
    min-height: 0;
  }

  .keyboard {
    gap: 3px;
    height: clamp(72px, 14svh, 96px);
  }

  .target-label {
    right: 7px;
    font-size: 0.62rem;
  }

  .mobile-best-record {
    font-size: 0.66rem;
    padding: 5px 8px;
  }

  .result-content {
    padding: 18px;
  }

  .record-input-row {
    grid-template-columns: 1fr;
  }

  .result-actions {
    grid-template-columns: 1fr;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .game-shell,
  body.is-immersive .game-shell,
  :fullscreen .game-shell,
  :-webkit-full-screen .game-shell {
    width: calc(100vw - 8px);
    height: 100vh;
    height: 100svh;
    min-height: 0;
    padding: 4px 0 max(4px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-rows: minmax(0, 1fr) clamp(92px, 24svh, 128px);
  }

  .topbar {
    position: fixed;
    top: max(6px, env(safe-area-inset-top));
    right: max(6px, env(safe-area-inset-right));
    z-index: 30;
    display: block;
  }

  .brand-block,
  .song-picker > span {
    display: none;
  }

  .controls {
    display: flex;
    gap: 6px;
  }

  .song-picker {
    width: min(34vw, 220px);
    grid-column: auto;
  }

  .song-menu-button {
    width: 100%;
  }

  .song-menu {
    width: min(34vw, 220px);
  }

  .song-menu-button,
  button {
    min-height: 36px;
  }

  .primary-btn,
  .secondary-btn {
    min-width: 72px;
    padding: 0 10px;
  }

  .icon-btn {
    width: 38px;
    min-height: 36px;
    background: rgba(24, 29, 37, 0.58);
    backdrop-filter: blur(8px);
  }

  .score-strip {
    position: fixed;
    left: max(6px, env(safe-area-inset-left));
    top: max(6px, env(safe-area-inset-top));
    z-index: 25;
    display: flex;
    gap: 8px;
    margin: 0;
    padding: 5px 8px;
    border-color: rgba(255, 255, 255, 0.1);
    background: rgba(24, 29, 37, 0.42);
    backdrop-filter: blur(8px);
  }

  .mobile-best-record {
    position: fixed;
    left: max(6px, env(safe-area-inset-left));
    top: max(6px, env(safe-area-inset-top));
    z-index: 26;
    max-width: min(32vw, 210px);
    margin: 0;
    padding: 5px 8px;
    font-size: 0.62rem;
    background: rgba(24, 29, 37, 0.5);
    backdrop-filter: blur(8px);
  }

  body.is-performing .live-score {
    left: calc(max(6px, env(safe-area-inset-left)) + min(32vw, 210px) + 7px);
    right: auto;
    top: max(6px, env(safe-area-inset-top));
    min-width: 72px;
    padding: 5px 7px;
  }

  .live-score span {
    font-size: 0.56rem;
  }

  .live-score strong {
    font-size: 0.86rem;
  }

  .score-strip {
    top: calc(max(6px, env(safe-area-inset-top)) + 29px);
  }

  .score-strip > div {
    min-width: 38px;
    gap: 1px;
  }

  .metric-label {
    font-size: 0.58rem;
  }

  .score-strip strong {
    font-size: 0.76rem;
  }

  .best-name {
    font-size: 0.56rem;
  }

  .stage-wrap {
    height: auto;
    min-height: 0;
  }

  .keyboard {
    height: auto;
    min-height: 92px;
    margin-top: 5px;
  }

  .piano-key {
    padding-bottom: 6px;
  }

  body.is-performing .score-strip,
  body.is-performing .song-picker,
  body.is-performing #startBtn,
  body.is-performing #demoBtn {
    display: none;
  }
}
