*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: #e9ecf5;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

body.idle { cursor: none; }
.ui { transition: opacity .6s ease; }
body.idle .ui { opacity: 0; pointer-events: none; }

/* ---------- top bar ---------- */
#topbar {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  display: flex;
  gap: 10px;
  z-index: 10;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, .12);
  background: rgba(14, 16, 28, .55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: #dfe3f0;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background .2s, border-color .2s, transform .2s;
}
.icon-btn:hover { background: rgba(40, 44, 70, .7); border-color: rgba(255, 255, 255, .25); }
.icon-btn:active { transform: scale(.95); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.small { width: 34px; height: 34px; border-radius: 10px; }
#btn-settings svg { transition: transform .6s ease; }
#btn-settings:hover svg { transform: rotate(60deg); }

/* ---------- status ---------- */
#status {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  letter-spacing: .01em;
  color: #b9bfd3;
  background: rgba(14, 16, 28, .45);
  border: 1px solid rgba(255, 255, 255, .08);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#status .dot { width: 8px; height: 8px; border-radius: 50%; background: #f2b84b; box-shadow: 0 0 10px #f2b84b; }
#status.ok .dot { background: #4be39a; box-shadow: 0 0 10px #4be39a; }
#status.err .dot { background: #ff5d6c; box-shadow: 0 0 10px #ff5d6c; }

/* ---------- hints ---------- */
#hint {
  position: fixed;
  left: 50%;
  top: 38%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  transition: opacity 1.2s ease, transform 1.2s ease;
  z-index: 5;
  width: min(92vw, 720px);
}
#hint.gone { opacity: 0; transform: translate(-50%, -60%); }
.hint-title {
  font-size: clamp(22px, 3.2vw, 38px);
  font-weight: 300;
  letter-spacing: .04em;
  color: #f1f3fb;
  text-shadow: 0 0 24px rgba(140, 160, 255, .45);
  animation: breathe 4s ease-in-out infinite;
}
.hint-sub { margin-top: 14px; font-size: 14px; color: #8a91aa; line-height: 1.8; }
kbd {
  font-family: inherit;
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(255, 255, 255, .05);
  color: #cfd4e6;
}
@keyframes breathe { 0%, 100% { opacity: .75; } 50% { opacity: 1; } }

#sound-hint {
  position: fixed;
  left: 50%;
  top: calc(max(16px, env(safe-area-inset-top)) + 52px);
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  color: #ffe3a8;
  background: rgba(60, 44, 10, .5);
  border: 1px solid rgba(255, 210, 120, .25);
  z-index: 11;
  pointer-events: none;
  transition: opacity .4s;
}
.hidden { opacity: 0; }

/* ---------- settings panel ---------- */
#panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: min(360px, 92vw);
  z-index: 20;
  background: rgba(10, 12, 22, .82);
  backdrop-filter: blur(18px) saturate(1.2);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
  border-left: 1px solid rgba(255, 255, 255, .08);
  box-shadow: -20px 0 60px rgba(0, 0, 0, .6);
  transform: translateX(105%);
  transition: transform .45s cubic-bezier(.2, .8, .2, 1);
  overflow-y: auto;
  padding: 0 22px 22px;
  user-select: auto;
}
#panel.open { transform: translateX(0); }
#panel header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0 12px;
  background: linear-gradient(rgba(10, 12, 22, .95) 70%, transparent);
  z-index: 1;
}
#panel h2 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: .02em; }
#panel h3 {
  margin: 22px 0 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: #7f87a3;
}
#panel section + section { border-top: 1px solid rgba(255, 255, 255, .06); }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 7px 0;
  font-size: 14px;
  color: #cfd4e6;
}
.row.check { justify-content: flex-start; gap: 10px; cursor: pointer; }
.row[hidden] { display: none; }

select, input[type="color"] {
  background: rgba(255, 255, 255, .06);
  color: #e9ecf5;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 8px;
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
  max-width: 190px;
}
select option { background: #12152a; }
input[type="color"] { width: 48px; height: 32px; padding: 2px; cursor: pointer; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 150px;
  height: 4px;
  border-radius: 4px;
  background: rgba(255, 255, 255, .14);
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 10px rgba(150, 170, 255, .8);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border: 0; border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 10px rgba(150, 170, 255, .8);
  cursor: pointer;
}
input[type="checkbox"] { width: 16px; height: 16px; accent-color: #8aa0ff; cursor: pointer; }

#device-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }
#device-list li { padding: 5px 0; color: #cfd4e6; display: flex; align-items: center; gap: 8px; }
#device-list li::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: #4be39a; box-shadow: 0 0 6px #4be39a; }
#device-list li.muted { color: #6f768f; }
#device-list li.muted::before { background: #555b70; box-shadow: none; }

#panel footer { margin-top: 26px; }
.text-btn {
  background: none;
  border: 1px solid rgba(255, 255, 255, .14);
  color: #b9bfd3;
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.text-btn:hover { border-color: rgba(255, 255, 255, .3); color: #fff; }

/* ---------- chord name ---------- */
#chord {
  --chord-scale: 1;
  --chord-color: #fff;
  --chord-glow: rgba(140, 160, 255, .8);
  position: fixed;
  left: 50%;
  top: 24%;
  transform: translate(-50%, -50%) scale(.96);
  text-align: center;
  pointer-events: none;
  z-index: 4;
  opacity: 0;
  transition: opacity .5s ease, transform .5s ease;
  white-space: nowrap;
}
#chord[data-pos="center"] { top: 42%; }
#chord[data-pos="keys"] { top: calc(var(--kb-top, 80vh) - 90px * var(--chord-scale)); }
#chord.show { opacity: 1; transform: translate(-50%, -50%) scale(1); transition-duration: .15s; }
#chord-name {
  font-size: calc(clamp(44px, 7vw, 96px) * var(--chord-scale));
  font-weight: 200;
  letter-spacing: .03em;
  line-height: 1;
  color: var(--chord-color);
  text-shadow: 0 0 18px var(--chord-glow), 0 0 50px var(--chord-glow);
}
#chord-name.empty { display: none; }
#chord-name.pop { animation: chordPop .35s cubic-bezier(.2, 1.4, .4, 1); }
#chord-notes {
  margin-top: calc(12px * var(--chord-scale));
  font-size: calc(14px * var(--chord-scale));
  letter-spacing: .18em;
  color: rgba(225, 230, 245, .65);
}
@keyframes chordPop {
  0% { transform: scale(.85); filter: brightness(1.8); }
  100% { transform: scale(1); filter: brightness(1); }
}

/* ---------- instrument toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(22vh + 20px);
  transform: translate(-50%, 10px);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 15px;
  letter-spacing: .04em;
  color: #f1f3fb;
  background: rgba(14, 16, 28, .7);
  border: 1px solid rgba(255, 255, 255, .14);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  z-index: 12;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.tip { font-size: 12px; color: #7f87a3; margin: 8px 0 0; line-height: 1.7; }
.tip[hidden] { display: none; }

.range-wrap { display: inline-flex; align-items: center; gap: 8px; }
.range-wrap input[type="range"] { width: 120px; }
.range-wrap output { min-width: 26px; text-align: right; font-size: 12px; color: #9aa2bd; font-variant-numeric: tabular-nums; }

/* ---------- record button ---------- */
.rec-btn .rec-glyph {
  width: 14px; height: 14px; border-radius: 50%;
  background: #ff4d5e;
  box-shadow: 0 0 10px rgba(255, 77, 94, .7);
  transition: border-radius .25s, transform .25s, background .25s;
}
.rec-btn.counting .rec-glyph { animation: recPulse 1s ease-in-out infinite; }
.rec-btn.recording { border-color: rgba(255, 77, 94, .55); background: rgba(70, 14, 22, .6); }
.rec-btn.recording .rec-glyph { border-radius: 3px; transform: scale(.9); background: #ff6b78; }
body.recording #topbar, body.playback #topbar { opacity: 1; pointer-events: auto; }
body.playback #btn-record { display: none; }

#rec-indicator {
  position: fixed;
  top: max(20px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 11;
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px; font-weight: 600; letter-spacing: .12em;
  color: #ffd6da;
  background: rgba(40, 8, 14, .6);
  border: 1px solid rgba(255, 77, 94, .35);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  font-variant-numeric: tabular-nums;
}
#rec-indicator[hidden], #countdown[hidden], #playbar[hidden], .modal-wrap[hidden] { display: none; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #ff3b4e; box-shadow: 0 0 12px #ff3b4e; animation: recBlink 1s steps(1) infinite; }
@keyframes recBlink { 50% { opacity: .15; } }
@keyframes recPulse { 50% { transform: scale(.6); opacity: .6; } }

/* ---------- countdown ---------- */
#countdown {
  position: fixed; inset: 0; z-index: 15;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 45%, rgba(60, 10, 20, .35), rgba(0, 0, 0, .55));
  pointer-events: none;
}
#countdown-num {
  font-size: min(34vh, 260px); font-weight: 200; line-height: 1;
  color: #fff;
  text-shadow: 0 0 40px rgba(255, 80, 100, .8), 0 0 100px rgba(255, 80, 100, .45);
}
#countdown-num.beat { animation: countBeat .9s ease-out; }
@keyframes countBeat { 0% { transform: scale(1.35); opacity: 0; } 15% { opacity: 1; } 100% { transform: scale(.92); opacity: .85; } }
.countdown-sub { margin-top: 10px; color: #c9cee0; font-size: 15px; letter-spacing: .06em; pointer-events: auto; display: flex; gap: 12px; align-items: center; }
.text-btn.small { padding: 5px 10px; font-size: 12px; }

/* ---------- modals ---------- */
.modal-wrap {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .55);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  animation: fadeIn .2s ease;
  padding: 16px;
  user-select: auto;
}
@keyframes fadeIn { from { opacity: 0; } }
.modal {
  width: min(640px, 100%);
  max-height: min(80vh, 760px);
  display: flex; flex-direction: column;
  background: rgba(12, 14, 26, .94);
  border: 1px solid rgba(255, 255, 255, .1);
  border-radius: 18px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .7), 0 0 0 1px rgba(120, 140, 255, .05);
  padding: 22px 24px;
  animation: popIn .25s cubic-bezier(.2, .9, .3, 1.1);
}
.modal.small { width: min(440px, 100%); }
@keyframes popIn { from { transform: translateY(10px) scale(.98); opacity: 0; } }
.modal h2 { margin: 0 0 6px; font-size: 19px; font-weight: 600; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.modal p { margin: 6px 0; }
.muted { color: #8a91aa; font-size: 14px; }
.muted.small, .small { font-size: 12px; }
.modal input[type="text"], .modal input[type="email"], .modal input[type="password"] {
  width: 100%;
  margin: 8px 0 4px;
  padding: 11px 13px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .14);
  background: rgba(255, 255, 255, .05);
  color: #eef0f8;
  font: inherit; font-size: 15px;
  outline: none;
}
.modal input:focus { border-color: rgba(138, 160, 255, .7); box-shadow: 0 0 0 3px rgba(138, 160, 255, .18); }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.btn {
  padding: 9px 15px; border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .16);
  background: rgba(255, 255, 255, .05);
  color: #e3e6f2; font: inherit; font-size: 14px; cursor: pointer;
  transition: background .2s, border-color .2s;
}
.btn:hover { background: rgba(255, 255, 255, .1); border-color: rgba(255, 255, 255, .28); }
.btn.primary { background: linear-gradient(180deg, #7f95ff, #5f73f0); border-color: transparent; color: #fff; }
.btn.primary:hover { background: linear-gradient(180deg, #91a4ff, #6a7ef5); }
.btn.ghost { border-color: transparent; background: none; color: #9aa2bd; }
.btn:disabled { opacity: .5; cursor: default; }
.auth-msg { min-height: 1.4em; font-size: 13px; color: #ff9aa5; margin-top: 10px !important; }
.auth-msg.ok { color: #7ff0b4; }

/* library */
.lib-account { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: #9aa2bd; margin: 2px 0 12px; }
.lib-tools { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
#lib-main { display: flex; flex-direction: column; min-height: 0; flex: 1; }
#lib-main[hidden], #lib-auth[hidden] { display: none; }
#lib-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 60px; }
#lib-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 11px 12px; border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, .06);
  background: rgba(255, 255, 255, .025);
  margin-bottom: 8px;
}
#lib-list li:hover { background: rgba(255, 255, 255, .05); }
#lib-list li.empty { color: #7f87a3; font-size: 14px; justify-content: center; text-align: center; padding: 26px 16px; }
.piece-info { min-width: 0; }
.piece-name { font-size: 15px; color: #eef0f8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.piece-meta { font-size: 12px; color: #7f87a3; margin-top: 3px; }
.piece-actions { display: flex; gap: 6px; flex-shrink: 0; }
.piece-btn {
  height: 32px; min-width: 32px; padding: 0 8px;
  display: inline-flex; align-items: center; gap: 6px;
  border-radius: 8px; border: 1px solid rgba(255, 255, 255, .12);
  background: rgba(255, 255, 255, .04); color: #cfd4e6; cursor: pointer; font: inherit; font-size: 13px;
}
.piece-btn:hover { background: rgba(255, 255, 255, .1); }
.piece-btn.primary { border-color: rgba(138, 160, 255, .45); color: #dfe5ff; }
.piece-btn svg, .pb-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.lib-foot { margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, .06); }

/* ---------- playback bar ---------- */
#playbar {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: 50%; transform: translateX(-50%);
  z-index: 9;
  width: min(560px, calc(100vw - 420px));
  min-width: 300px;
  text-align: center;
}
#pb-title {
  font-size: clamp(18px, 2.4vw, 28px); font-weight: 300; letter-spacing: .05em;
  color: #f3f5fc; text-shadow: 0 0 20px rgba(140, 160, 255, .5);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#pb-progress { margin: 10px auto 8px; height: 4px; border-radius: 4px; background: rgba(255, 255, 255, .12); cursor: pointer; overflow: hidden; }
#pb-fill { height: 100%; width: 0; background: linear-gradient(90deg, #7f95ff, #b9a2ff); box-shadow: 0 0 10px rgba(140, 160, 255, .8); }
#pb-controls { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; transition: opacity .6s; }
body.idle #pb-controls { opacity: .25; }
.pb-btn {
  width: 32px; height: 32px; border-radius: 9px;
  display: grid; place-items: center;
  border: 1px solid rgba(255, 255, 255, .12); background: rgba(14, 16, 28, .55); color: #dfe3f0; cursor: pointer;
}
.pb-btn:hover { background: rgba(40, 44, 70, .7); }
#pb-play .i-play { display: none; }
#pb-play.paused .i-play { display: block; }
#pb-play.paused .i-pause { display: none; }
.seg { display: inline-flex; border: 1px solid rgba(255, 255, 255, .12); border-radius: 9px; overflow: hidden; }
.seg-btn { padding: 6px 12px; background: rgba(14, 16, 28, .55); color: #b9bfd3; border: 0; font: inherit; font-size: 13px; cursor: pointer; }
.seg-btn.active { background: rgba(127, 149, 255, .35); color: #fff; }
#pb-speed { padding: 5px 6px; font-size: 13px; border-radius: 9px; background: rgba(14, 16, 28, .55); color: #e9ecf5; border: 1px solid rgba(255, 255, 255, .12); }
#pb-score { font-size: 12px; color: #9aa2bd; font-variant-numeric: tabular-nums; min-width: 0; }

/* ---------- metronome ---------- */
.metro-btn { position: relative; }
.metro-btn .metro-arm { transform-origin: 12px 15px; transition: transform .12s ease-out; }
.metro-btn.on { border-color: rgba(140, 170, 255, .5); background: rgba(30, 40, 80, .65); color: #cfdcff; }
.metro-btn.on .metro-arm { transform: rotate(-38deg); }
.metro-btn.on.tock .metro-arm { transform: rotate(0deg); }
.metro-btn.beat { box-shadow: 0 0 0 1px rgba(160, 190, 255, .35), 0 0 14px rgba(120, 160, 255, .35); }
.metro-btn.beat.accent { box-shadow: 0 0 0 1px rgba(200, 220, 255, .6), 0 0 20px rgba(140, 180, 255, .6); }
.metro-bpm {
  position: absolute; bottom: -15px; left: 50%; transform: translateX(-50%);
  font: 600 10px/1 system-ui, sans-serif; letter-spacing: .04em; color: #a9b8e8;
  opacity: 0; transition: opacity .2s; pointer-events: none; white-space: nowrap;
}
.metro-btn.on .metro-bpm { opacity: .85; }
.metro-btn.on.sound { border-color: rgba(170, 200, 255, .8); background: rgba(45, 60, 120, .75); color: #fff; }
.metro-actions { display: flex; gap: 8px; justify-content: flex-start; }
.btn.small { padding: 6px 12px; font-size: 13px; }
