:root {
  --ink: #172031;
  --muted: #64708a;
  --paper: #fffdf3;
  --line: rgba(23, 32, 49, 0.18);
  --green: #42c88a;
  --mint: #8fe8d2;
  --cyan: #39b9e5;
  --yellow: #ffd45a;
  --coral: #ff765f;
  --pink: #ff8ab6;
  --violet: #7a6cff;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: #f7f2de;
  color: var(--ink);
  font-family: Inter, ui-rounded, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
  touch-action: manipulation;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  position: relative;
  width: 100vw;
  height: 100dvh;
  min-height: 520px;
  overflow: hidden;
  isolation: isolate;
  background:
    linear-gradient(120deg, rgba(255, 118, 95, 0.2), transparent 28%),
    linear-gradient(250deg, rgba(57, 185, 229, 0.28), transparent 38%),
    linear-gradient(180deg, #fff8dd 0%, #dff4d8 45%, #b8e6e0 100%);
}

.app::before {
  position: absolute;
  inset: 0;
  z-index: -2;
  content: "";
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.28) 0 1px, transparent 1px 72px),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.2) 0 1px, transparent 1px 72px);
  opacity: 0.7;
}

#stage {
  position: absolute;
  inset: 0;
  z-index: -1;
  width: 100%;
  height: 100%;
}

.topbar {
  position: relative;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: min(1120px, calc(100vw - 32px));
  min-height: 72px;
  margin: 18px auto 0;
  padding: 12px 14px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  font-size: clamp(1.05rem, 1.6vw, 1.35rem);
  font-weight: 850;
  letter-spacing: 0;
}

.brand-mark {
  position: relative;
  width: 40px;
  height: 40px;
  border: 3px solid rgba(23, 32, 49, 0.22);
  border-radius: 8px;
  background:
    linear-gradient(135deg, var(--yellow) 0 42%, var(--coral) 42% 65%, var(--cyan) 65% 100%);
  box-shadow: 0 10px 24px rgba(23, 32, 49, 0.16);
  transform: rotate(-5deg);
}

.brand-mark::after {
  position: absolute;
  right: -8px;
  bottom: -8px;
  width: 17px;
  height: 17px;
  border: 3px solid var(--paper);
  border-radius: 50%;
  background: var(--green);
  content: "";
}

.status-strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.status-pill {
  display: grid;
  grid-template-columns: auto minmax(42px, auto);
  align-items: baseline;
  gap: 10px;
  min-width: 112px;
  padding: 10px 13px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.72);
  box-shadow: 0 12px 24px rgba(23, 32, 49, 0.08);
  backdrop-filter: blur(12px);
}

.status-pill span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
}

.status-pill strong {
  font-size: 1.15rem;
  font-weight: 900;
  text-align: right;
}

.play-field {
  position: absolute;
  inset: 98px 0 86px;
  z-index: 2;
  display: grid;
  place-items: center;
  outline: none;
  user-select: none;
}

.pulse-ring {
  position: fixed;
  top: 50%;
  left: 50%;
  width: min(72vmin, 620px);
  aspect-ratio: 1;
  border: 5px solid rgba(255, 255, 255, 0.64);
  border-radius: 50%;
  box-shadow: inset 0 0 50px rgba(255, 255, 255, 0.35);
  opacity: 0.58;
  transform: translate(-50%, -50%) scale(0.9);
}

.ring-two {
  width: min(54vmin, 470px);
  border-color: rgba(255, 212, 90, 0.72);
  animation: breathe 5s ease-in-out infinite;
}

.ring-three {
  width: min(38vmin, 340px);
  border-color: rgba(57, 185, 229, 0.55);
  animation: breathe 3.7s ease-in-out infinite reverse;
}

@keyframes breathe {
  50% {
    transform: translate(-50%, -50%) scale(1.07);
    opacity: 0.36;
  }
}

.key-bubble {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: 2;
  display: grid;
  place-items: center;
  min-width: min(54vw, 430px);
  max-width: calc(100vw - 40px);
  min-height: min(32vmin, 270px);
  padding: 28px;
  border: 5px solid rgba(23, 32, 49, 0.13);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.94), rgba(255, 255, 255, 0.58)),
    linear-gradient(90deg, rgba(255, 212, 90, 0.25), rgba(57, 185, 229, 0.24));
  color: var(--ink);
  box-shadow: 0 32px 80px rgba(23, 32, 49, 0.18);
  font-size: clamp(4rem, 17vmin, 11rem);
  font-weight: 950;
  letter-spacing: 0;
  line-height: 0.92;
  text-align: center;
  text-transform: uppercase;
  transform: translate(-50%, -50%) rotate(-2deg);
  transition: transform 130ms ease, background 180ms ease;
}

.key-bubble.pop {
  transform: translate(-50%, -50%) scale(1.07) rotate(2deg);
}

.note-bubble {
  position: fixed;
  left: 50%;
  top: calc(50% + min(22vmin, 190px));
  z-index: 3;
  min-width: 180px;
  max-width: calc(100vw - 48px);
  padding: 12px 18px;
  border: 2px solid rgba(23, 32, 49, 0.12);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.76);
  color: var(--muted);
  box-shadow: 0 16px 40px rgba(23, 32, 49, 0.1);
  font-size: clamp(0.95rem, 2vw, 1.12rem);
  font-weight: 800;
  text-align: center;
  transform: translateX(-50%);
  backdrop-filter: blur(10px);
}

.key-trail {
  position: fixed;
  right: max(22px, 4vw);
  bottom: max(22px, 4vw);
  z-index: 3;
  display: flex;
  flex-direction: column-reverse;
  align-items: flex-end;
  gap: 8px;
  width: 140px;
  pointer-events: none;
}

.touch-keys {
  position: fixed;
  right: 18px;
  bottom: 92px;
  left: 18px;
  z-index: 6;
  display: none;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 8px;
  pointer-events: auto;
}

.touch-keys button {
  height: 58px;
  border: 3px solid rgba(23, 32, 49, 0.12);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.78);
  color: var(--ink);
  box-shadow: 0 12px 28px rgba(23, 32, 49, 0.12);
  font-size: 1.25rem;
  font-weight: 950;
}

.trail-chip {
  display: grid;
  place-items: center;
  min-width: 44px;
  height: 38px;
  padding: 0 12px;
  border: 2px solid rgba(23, 32, 49, 0.16);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.82);
  box-shadow: 0 10px 18px rgba(23, 32, 49, 0.1);
  font-weight: 900;
  transform: translateX(var(--chip-offset, 0));
}

.lower-rail {
  position: absolute;
  right: 18px;
  bottom: 14px;
  left: 18px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px;
  pointer-events: none;
}

.song-control {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 2px solid rgba(23, 32, 49, 0.13);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.74);
  box-shadow: 0 18px 38px rgba(23, 32, 49, 0.09);
  backdrop-filter: blur(10px);
  pointer-events: auto;
}

.exit-dock,
.volume-dock,
.speed-dock {
  position: relative;
  display: inline-flex;
  flex: 0 0 auto;
  pointer-events: auto;
}

.exit-gate-button,
.volume-gate-button,
.speed-gate-button {
  position: relative;
  display: grid;
  width: 50px;
  height: 50px;
  min-height: 50px;
  place-items: center;
  border: 2px solid rgba(23, 32, 49, 0.13);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.82);
  box-shadow: 0 18px 38px rgba(23, 32, 49, 0.09);
  cursor: pointer;
  backdrop-filter: blur(10px);
}

.exit-gate-button {
  color: var(--ink);
  font-size: 1.45rem;
  font-weight: 950;
  line-height: 1;
}

.speed-gate-button {
  color: var(--ink);
  font-size: 1.45rem;
  font-weight: 950;
  line-height: 1;
}

.gate-click-count,
.start-click-count {
  position: absolute;
  right: -6px;
  top: -6px;
  display: none;
  min-width: 22px;
  height: 22px;
  place-items: center;
  border: 2px solid white;
  border-radius: 999px;
  background: var(--coral);
  color: white;
  font-size: 0.75rem;
  font-weight: 950;
  line-height: 1;
}

.gate-click-count:not(:empty),
.start-click-count:not(:empty) {
  display: grid;
}

.volume-dock.is-open .gate-click-count {
  display: none;
}

.speed-dock.is-open .gate-click-count {
  display: none;
}

.volume-popover,
.speed-popover {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 0;
  width: min(280px, calc(100vw - 36px));
  padding: 12px;
  border: 2px solid rgba(23, 32, 49, 0.13);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.94);
  box-shadow: 0 22px 52px rgba(23, 32, 49, 0.16);
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 140ms ease, transform 140ms ease;
  backdrop-filter: blur(12px);
}

.volume-dock.is-open .volume-popover,
.speed-dock.is-open .speed-popover {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.volume-popover label,
.speed-popover label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.volume-popover div,
.speed-popover div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}

.volume-popover input,
.speed-popover input {
  width: 100%;
  min-width: 0;
  accent-color: var(--coral);
}

.volume-popover strong,
.speed-popover strong {
  min-width: 46px;
  text-align: right;
  font-weight: 950;
}

.song-control {
  min-width: min(440px, calc(100vw - 36px));
}

.song-button {
  min-height: 40px;
  padding: 0 16px;
  white-space: nowrap;
}

.song-control select {
  width: 100%;
  min-width: 180px;
  min-height: 40px;
  border: 2px solid rgba(23, 32, 49, 0.12);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  font-weight: 850;
}

.speaker-icon {
  position: relative;
  width: 28px;
  height: 24px;
  flex: 0 0 28px;
}

.speaker-icon::before {
  position: absolute;
  top: 6px;
  left: 0;
  width: 10px;
  height: 12px;
  border-radius: 3px;
  background: var(--ink);
  content: "";
}

.speaker-icon::after {
  position: absolute;
  top: 3px;
  left: 8px;
  width: 16px;
  height: 18px;
  clip-path: polygon(0 34%, 100% 0, 100% 100%, 0 66%);
  background: var(--ink);
  content: "";
}

.start-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(255, 253, 243, 0.62);
  backdrop-filter: blur(12px);
}

.start-overlay.is-hidden {
  display: none;
}

.start-panel {
  display: grid;
  gap: 18px;
  width: min(440px, calc(100vw - 40px));
  padding: 26px;
  border: 2px solid rgba(23, 32, 49, 0.13);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.86);
  box-shadow: 0 24px 70px rgba(23, 32, 49, 0.18);
  text-align: center;
}

.start-title {
  font-size: clamp(2rem, 7vw, 3.1rem);
  font-weight: 950;
  letter-spacing: 0;
}

.start-help {
  margin: -4px 0 0;
  color: #46516a;
  font-size: 0.98rem;
  font-weight: 750;
  line-height: 1.45;
  text-align: left;
}

.start-help strong {
  color: var(--ink);
  font-weight: 950;
}

.primary-action,
.secondary-action,
.icon-button {
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  font-weight: 900;
  cursor: pointer;
}

.primary-action {
  position: relative;
  padding: 0 22px;
  background: var(--ink);
  color: white;
  box-shadow: 0 14px 28px rgba(23, 32, 49, 0.18);
}

.secondary-action {
  display: inline-grid;
  place-items: center;
  padding: 0 22px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  background: rgba(255, 255, 255, 0.66);
  color: var(--ink);
  text-align: center;
  text-decoration: none;
}

.icon-button {
  display: grid;
  width: 44px;
  height: 44px;
  min-height: 44px;
  place-items: center;
  border: 2px solid rgba(23, 32, 49, 0.12);
  background: rgba(255, 255, 255, 0.66);
  color: var(--ink);
  font-size: 1.25rem;
  line-height: 1;
}

.parent-panel {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(23, 32, 49, 0.34);
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px);
  transition: opacity 180ms ease, transform 180ms ease;
  backdrop-filter: blur(10px);
}

.parent-panel.is-open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.report-panel {
  width: min(520px, calc(100vw - 36px));
  max-height: calc(100dvh - 44px);
  overflow: auto;
  padding: 22px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 28px 80px rgba(23, 32, 49, 0.28);
}

.report-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.report-header h1 {
  margin: 0;
  font-size: clamp(1.7rem, 5vw, 2.5rem);
  line-height: 1.05;
  letter-spacing: 0;
}

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

.report-grid div {
  min-width: 0;
  padding: 14px;
  border: 2px solid rgba(23, 32, 49, 0.11);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.7);
}

.report-grid dt {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 850;
  text-transform: uppercase;
}

.report-grid dd {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 1.45rem;
  font-weight: 950;
}

.prize-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  margin: 18px 0;
  padding: 12px 14px;
  border-left: 5px solid var(--green);
  border-radius: 8px;
  background: rgba(66, 200, 138, 0.13);
  color: #24523f;
  font-weight: 800;
}

.prize-card div {
  font-size: 2.2rem;
  line-height: 1;
}

.prize-card p {
  margin: 0;
}

.volume-setting label,
.speed-setting label,
.praise-setting label,
.playlist-setting label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 850;
  text-transform: uppercase;
}

.volume-setting,
.speed-setting,
.praise-setting {
  display: grid;
  gap: 8px;
  margin: 18px 0;
}

.volume-setting div,
.speed-setting div {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 8px 12px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  border-radius: 8px;
  background: white;
}

.volume-setting input,
.speed-setting input {
  min-width: 0;
  accent-color: var(--coral);
}

.volume-setting strong,
.speed-setting strong {
  min-width: 46px;
  text-align: right;
  font-weight: 950;
}

.praise-actions {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 8px;
}

.praise-actions input {
  min-width: 0;
  min-height: 48px;
  padding: 10px 12px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  font-weight: 800;
}

.praise-actions button {
  min-height: 48px;
  padding: 0 14px;
}

.praise-actions button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.praise-setting p {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 800;
}

.playlist-setting {
  display: grid;
  gap: 8px;
  margin: 18px 0;
}

.playlist-setting input {
  min-width: 0;
  min-height: 46px;
  padding: 0 14px;
  border: 2px solid rgba(23, 32, 49, 0.14);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  font-weight: 800;
}

.playlist-setting input[type="file"] {
  padding: 10px 14px;
}

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

.report-actions .primary-action,
.report-actions .secondary-action {
  width: 100%;
  min-width: 0;
}

.floating-icon {
  position: fixed;
  z-index: 4;
  display: grid;
  place-items: center;
  width: clamp(52px, 10vmin, 98px);
  height: clamp(52px, 10vmin, 98px);
  border: 3px solid rgba(23, 32, 49, 0.12);
  border-radius: 8px;
  background: rgba(255, 253, 243, 0.78);
  box-shadow: 0 18px 44px rgba(23, 32, 49, 0.16);
  font-size: clamp(2rem, 7vmin, 4.5rem);
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.8) rotate(var(--icon-tilt));
  animation: floatIcon 1250ms ease-out forwards;
}

@keyframes floatIcon {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.72) rotate(var(--icon-tilt));
  }

  16% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    transform: translate(
      calc(-50% + var(--icon-drift-x)),
      calc(-50% - 150px)
    ) scale(1.16) rotate(calc(var(--icon-tilt) + var(--icon-spin)));
  }
}

.is-flashing .key-bubble {
  animation: noExit 360ms ease;
}

@keyframes noExit {
  0%,
  100% {
    transform: translate(-50%, -50%) rotate(-2deg);
  }

  25% {
    transform: translate(calc(-50% - 16px), -50%) rotate(-4deg);
  }

  75% {
    transform: translate(calc(-50% + 16px), -50%) rotate(3deg);
  }
}

@media (max-width: 720px) {
  .app {
    min-height: 480px;
  }

  .topbar {
    width: calc(100vw - 20px);
    min-height: 64px;
    margin-top: 8px;
    padding: 8px 4px;
  }

  .brand span:last-child {
    display: none;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
  }

  .status-pill {
    min-width: 92px;
    padding: 8px 10px;
  }

  .play-field {
    inset: 78px 0 250px;
  }

  .key-bubble {
    min-width: calc(100vw - 42px);
    min-height: 220px;
  }

  .note-bubble {
    top: calc(50% + 140px);
  }

  .key-trail {
    display: none;
  }

  .touch-keys {
    display: grid;
    right: 10px;
    bottom: calc(max(8px, env(safe-area-inset-bottom)) + 142px);
    left: 10px;
    z-index: 20;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
  }

  .touch-keys button {
    height: 48px;
  }

  .lower-rail {
    position: fixed;
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(3, 58px);
    grid-template-areas:
      "song song song"
      "exit volume speed";
    justify-content: center;
    gap: 8px 12px;
  }

  .volume-popover,
  .speed-popover {
    position: fixed;
    right: 10px;
    bottom: calc(max(8px, env(safe-area-inset-bottom)) + 126px);
    left: 10px;
    z-index: 60;
    width: auto;
  }

  .exit-dock {
    grid-area: exit;
  }

  .volume-dock {
    grid-area: volume;
  }

  .speed-dock {
    grid-area: speed;
  }

  .exit-gate-button,
  .volume-gate-button,
  .speed-gate-button {
    width: 58px;
    height: 58px;
    min-height: 58px;
  }

  .song-control {
    grid-area: song;
    width: 100%;
    min-width: 0;
    padding: 8px;
    display: grid;
    grid-template-columns: minmax(104px, 0.82fr) minmax(0, 1.18fr);
    gap: 8px;
  }

  .song-button {
    min-height: 48px;
    padding: 0 12px;
    font-size: 0.95rem;
  }

  .song-control select {
    min-width: 0;
    min-height: 48px;
    font-size: 0.95rem;
  }

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

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

@media (pointer: coarse) {
  .touch-keys {
    display: grid;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
