:root {
  --bg: #f8f8f5;
  --surface: #ffffff;
  --surface-muted: #f1f1ec;
  --border: #dcdcd5;
  --border-strong: #c4c4ba;
  --text: #181816;
  --text-muted: #64645d;
  --primary: #1a1a18;
  --primary-fg: #ffffff;
  --hover: #efefea;

  --hand-r: #d9480f;
  --hand-r-soft: rgba(217, 72, 15, 0.14);
  --hand-l: #1971c2;
  --hand-l-soft: rgba(25, 113, 194, 0.14);

  --good: #2b8a3e;
  --warn: #b36b00;
  --bad: #c92a2a;
  --ring: #52524c;
  --radius: 7px;
}

[data-theme="dark"] {
  --bg: #131517;
  --surface: #1b1e21;
  --surface-muted: #23272b;
  --border: #2f343a;
  --border-strong: #434a52;
  --text: #ededeb;
  --text-muted: #9b9fa6;
  --primary: #ededeb;
  --primary-fg: #131517;
  --hover: #282c31;

  --hand-r: #ff8743;
  --hand-r-soft: rgba(255, 135, 67, 0.18);
  --hand-l: #5c9ce6;
  --hand-l-soft: rgba(92, 156, 230, 0.18);

  --good: #51cf66;
  --warn: #fcc419;
  --bad: #ff6b6b;
  --ring: #adb5bd;
}

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

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 14px 16px 36px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 4px;
}

.topbar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

.meta-stat {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* Buttons & Controls */
.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  white-space: nowrap;
  transition: background-color 0.1s ease, border-color 0.1s ease;
}

.btn:hover:not(:disabled) {
  background: var(--hover);
  border-color: var(--border-strong);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.is-on {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.9;
  background: var(--primary);
  border-color: var(--primary);
}

.btn-play {
  min-width: 80px;
  padding: 8px 16px;
  font-size: 14px;
}

.btn-play.is-playing {
  background: var(--bad);
  border-color: var(--bad);
  color: #ffffff;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-xs {
  padding: 3px 8px;
  font-size: 12px;
}

.seg {
  display: inline-flex;
  flex-wrap: nowrap;
  gap: 4px;
  align-items: center;
}

.seg.wrap {
  flex-wrap: wrap;
}

input[type="number"],
input[type="text"],
select {
  font: inherit;
  font-size: 13px;
  padding: 5px 8px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

input[type="number"]:focus,
input[type="text"]:focus,
select:focus {
  outline: 2px solid var(--ring);
  outline-offset: 1px;
}

input[type="range"] {
  accent-color: var(--primary);
  cursor: pointer;
}

.chk {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.chk input[type="checkbox"] {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--primary);
  cursor: pointer;
}

.field-label,
.row-label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Transport */
.transport {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}

.transport-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
}

.bpm-box,
.timesig-box,
.audio-opts {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.bpm-controls {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

#bpmInput {
  width: 60px;
  text-align: center;
  font-weight: 700;
  font-size: 15px;
  padding: 4px 4px;
}

.bpm-note {
  font-size: 11px;
  color: var(--text-muted);
}

.vol-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

#volumeRange {
  width: 82px;
}

/* Stage / Notation */
.stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
}

.stage-status {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.beat-lights {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.beat-light {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  background: var(--surface-muted);
}

.beat-light.is-active {
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
}

.beat-light.is-first.is-active {
  background: var(--hand-r);
  border-color: var(--hand-r);
  color: #ffffff;
}

.status-tag {
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text);
}

.status-tag.phase-demo {
  border-color: var(--hand-l);
  color: var(--hand-l);
}

.status-tag.phase-response {
  border-color: var(--hand-r);
  color: var(--hand-r);
}

.status-tag.phase-dropped {
  border-color: var(--warn);
  color: var(--warn);
}

.bar-counter {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.stage-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* Notation container */
.notation-box {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 10px;
  background: var(--bg);
  min-height: 152px;
}

.notation-content.is-blind {
  visibility: hidden;
  user-select: none;
}

.blind-cover {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--surface-muted);
  border-radius: var(--radius);
  text-align: center;
  padding: 16px;
}

.blind-cover[hidden] {
  display: none;
}

.blind-cover p {
  margin: 0;
  font-size: 14px;
  color: var(--text);
}

/* Design B: Split Staff SVG */
.staff-svg {
  display: block;
  width: 100%;
  height: auto;
  color: var(--text);
  user-select: none;
}

.staff-svg[hidden],
.staff-svg.is-hidden {
  display: none;
}

.staff-note {
  cursor: pointer;
}

.staff-note.hand-R {
  --hand: var(--hand-r);
}

.staff-note.hand-L {
  --hand: var(--hand-l);
}

.staff-note.is-rest {
  --hand: var(--text-muted);
}

.staff-note .note-cursor {
  fill: var(--hand);
  opacity: 0;
}

.staff-note.is-current .note-cursor {
  opacity: 0.28;
}

.staff-note:hover .note-hit-area {
  fill: rgba(128, 128, 128, 0.08);
}

.staff-note .hand-label-btn {
  cursor: pointer;
}

.staff-note .hand-label-btn:hover rect {
  fill: rgba(128, 128, 128, 0.14);
}

/* Design C: Grid Notation */
.grid-view {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.grid-view[hidden] {
  display: none;
}

.grid-beat {
  flex: 1 1 160px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.grid-beat-head {
  font-size: 11px;
  color: var(--text-muted);
}

.grid-beat-cells {
  display: flex;
  gap: 3px;
}

.grid-cell {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  padding: 6px 1px 7px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: manipulation;
  font-family: inherit;
}

.grid-cell.hand-R {
  --hand: var(--hand-r);
}

.grid-cell.hand-L {
  --hand: var(--hand-l);
}

.grid-cell .cell-count {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.1;
}

.grid-cell .cell-hand {
  font-size: 19px;
  font-weight: 700;
  color: var(--hand);
  line-height: 1.15;
  min-height: 22px;
  padding: 0 3px;
  border-radius: 4px;
}

.grid-beat-cells.sub-6 .cell-hand {
  font-size: 15px;
}

.grid-beat-cells.sub-8 .cell-hand {
  font-size: 13px;
}

.grid-beat-cells.sub-8 .cell-count {
  font-size: 9px;
}

.grid-cell.is-accent {
  background: var(--hand);
  border-color: var(--hand);
}

.grid-cell.is-accent .cell-count,
.grid-cell.is-accent .cell-hand {
  color: #ffffff;
}

.grid-cell.is-rest {
  background: transparent;
  border-style: dashed;
  border-color: var(--border);
}

.grid-cell.is-rest .cell-count {
  opacity: 0.65;
}

.grid-cell.is-current {
  outline: 3px solid var(--ring);
  outline-offset: 1px;
}

.grid-view.is-compact .grid-cell {
  padding: 4px 1px;
}

.grid-view.is-compact .cell-hand {
  font-size: 14px;
  min-height: 17px;
}

/* Preview Box */
.preview-box {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 8px 10px;
  background: var(--surface-muted);
}

.preview-box[hidden] {
  display: none;
}

.preview-head {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.notation-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 12px;
}

.hint {
  font-size: 12px;
  color: var(--text-muted);
}

.tap-mode-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

.tap-mode-wrap[hidden] {
  display: none;
}

/* Judgment & Pads */
.judge-section {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.judge-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
}

.judge-live {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.judge-badge {
  min-width: 112px;
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: var(--radius);
  background: var(--surface-muted);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.judge-badge.is-good {
  color: var(--good);
  border-color: var(--good);
}

.judge-badge.is-warn {
  color: var(--warn);
  border-color: var(--warn);
}

.judge-badge.is-bad {
  color: var(--bad);
  border-color: var(--bad);
}

.timing-meter {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.meter-label {
  font-size: 11px;
  color: var(--text-muted);
}

.meter-track {
  position: relative;
  width: 130px;
  height: 10px;
  border-radius: 5px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
}

.meter-good-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 37%;
  right: 37%;
  background: rgba(43, 138, 62, 0.16);
}

.meter-center {
  position: absolute;
  top: -2px;
  bottom: -2px;
  left: 50%;
  width: 1px;
  background: var(--border-strong);
}

.meter-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: left 0.06s linear;
}

.meter-dot.is-visible {
  opacity: 1;
}

.meter-dot.is-good {
  background: var(--good);
}

.meter-dot.is-warn {
  background: var(--warn);
}

.meter-dot.is-bad {
  background: var(--bad);
}

.judge-summary {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.pads {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.pad {
  height: 76px;
  border-radius: var(--radius);
  border: 2px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-family: inherit;
  transition: transform 0.04s ease, background-color 0.06s ease;
}

.pad-l {
  border-color: var(--hand-l);
}

.pad-r {
  border-color: var(--hand-r);
}

.pad-title {
  font-size: 16px;
  font-weight: 700;
}

.pad-l .pad-title {
  color: var(--hand-l);
}

.pad-r .pad-title {
  color: var(--hand-r);
}

.pad-key {
  font-size: 12px;
  color: var(--text-muted);
}

.pad.is-pressed.pad-l {
  background: var(--hand-l-soft);
  transform: scale(0.985);
}

.pad.is-pressed.pad-r {
  background: var(--hand-r-soft);
  transform: scale(0.985);
}

/* Bottom Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ctrl-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.ctrl-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ctrl-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.ctrl-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.head-actions,
.hand-mute-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.hand-r-text {
  color: var(--hand-r);
  font-weight: 600;
}

.hand-l-text {
  color: var(--hand-l);
  font-weight: 600;
}

.ctrl-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.ctrl-row .row-label {
  min-width: 56px;
}

.per-beat-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.beat-sub-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-muted);
}

.beat-sub-item select {
  padding: 4px 6px;
  font-size: 13px;
}

.mode-help {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

/* Random */
.random-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chk-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.saved-section {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.save-form {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.save-form input[type="text"] {
  flex: 1 1 180px;
  min-width: 0;
}

.saved-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.saved-empty {
  font-size: 12px;
  color: var(--text-muted);
}

.saved-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}

.saved-info {
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

.saved-name {
  font-size: 13px;
  font-weight: 600;
}

.saved-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.saved-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

/* Practice Assistants & Calibration */
.assist-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.inline-inputs {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
  font-size: 13px;
}

.inline-inputs input[type="number"] {
  width: 58px;
  text-align: center;
  padding: 4px;
}

.sub-info {
  font-size: 12px;
  color: var(--text-muted);
}

.latency-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

#latencyRange {
  flex: 1 1 140px;
}

.cal-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.cal-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.cal-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--border-strong);
  flex-shrink: 0;
}

.cal-dot.is-flash {
  background: var(--hand-r);
}

.cal-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.cal-result {
  font-size: 13px;
  font-weight: 600;
  margin-right: auto;
}

/* Responsive (Mobile <= 640px) */
@media (max-width: 640px) {
  .app {
    padding: 10px 10px 28px;
    gap: 10px;
  }

  .ctrl-grid-2 {
    grid-template-columns: 1fr;
  }

  .transport-row {
    gap: 8px 10px;
  }

  .btn-play {
    flex: 1 1 100%;
  }

  .bpm-box {
    width: 100%;
    justify-content: space-between;
  }

  .bpm-controls {
    flex-wrap: nowrap;
  }

  .stage {
    padding: 10px;
  }

  .notation-box {
    padding: 8px 4px;
  }

  .grid-beat {
    flex: 1 1 calc(50% - 6px);
  }

  .pad {
    height: 86px;
  }

  .ctrl-row .row-label {
    min-width: 48px;
  }

  #subAllGroup {
    flex-wrap: wrap;
  }

  .meter-track {
    width: 100px;
  }
}
