:root, [data-theme="dark"] {
  --bg: #141414;
  --panel: #1b1b1b;
  --panel-border: #262626;
  --btn: #242424;
  --btn-text: #edeae3;
  --text: #edeae3;
  --muted: #b5b5b5;
  --ink: #edeae3;
  --accent: #f5a524;
  --accent-strong: #ffb02e;
  --accent-text: #f5a524;
  --accent-bg: #2a2110;
  --on-accent: #141414;
  --toggle-off-border: #333;
  --overlay: rgba(0, 0, 0, 0.6);
  --card: #141414;
  --posture-bg: #123a22;
  --posture-text: #4ade80;
  --posture-glow: 74, 222, 128;
}
[data-theme="light"] {
  --bg: #f7f5f0;
  --panel: #ece8e0;
  --panel-border: #d6d0c4;
  --btn: #ded8cc;
  --btn-text: #1a1a1a;
  --text: #1a1a1a;
  --muted: #555;
  --ink: #1a1a1a;
  --accent: #e8960c;
  --accent-strong: #c25e00;
  --accent-text: #8a5300;
  --accent-bg: #fbe3b5;
  --on-accent: #1a1a1a;
  --toggle-off-border: #b8b0a2;
  --overlay: rgba(40, 30, 10, 0.35);
  --card: #f7f5f0;
  --posture-bg: #d3f2dd;
  --posture-text: #166534;
  --posture-glow: 22, 163, 74;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---- music ---- */
#music {
  flex: 1;
  min-width: 0;
  position: relative;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  overflow: hidden;
}
.wrap { position: relative; }
.line { color: var(--ink); line-height: 0; }
.line svg { display: block; }
.line .ev.now { color: var(--accent-strong); }
.roll { animation: rollUp 0.2s ease-out; }
.fadein { animation: fadeIn 0.3s ease-out; }
@keyframes rollUp { from { transform: translateY(var(--roll, 100px)); } to { transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

#cursor {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 0;
  pointer-events: none;
  will-change: transform;
  opacity: 0;
  transition: opacity 0.2s;
}
#cursor.on { opacity: 1; }
#cursor .band {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(var(--band, 30px) / -2);
  width: var(--band, 30px);
  background: var(--accent);
  opacity: 0.2;
  border-radius: 8px;
}
#cursor .bar {
  position: absolute;
  top: 10px;
  bottom: 4px;
  left: -2.5px;
  width: 5px;
  border-radius: 3px;
  background: var(--accent);
}
#cursor .tip {
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 10px solid var(--accent);
}

#posture {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px;
  border-radius: 999px;
  background: var(--posture-bg);
  color: var(--posture-text);
  font-weight: 600;
  font-size: 15px;
  animation: pulse 1.6s ease-in-out infinite;
  z-index: 3;
}
#posture[hidden] { display: none; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(var(--posture-glow), 0.55); }
  50% { box-shadow: 0 0 0 12px rgba(var(--posture-glow), 0); }
}

#countBig {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 120px;
  font-weight: 800;
  color: var(--accent);
  text-shadow: 0 2px 30px var(--bg);
  pointer-events: none;
  z-index: 2;
}
#countBig[hidden] { display: none; }

/* ---- panel ---- */
#panel {
  width: 176px;
  flex: none;
  background: var(--panel);
  border-left: 1px solid var(--panel-border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
}
.btn {
  height: 44px;
  border: none;
  border-radius: 10px;
  background: var(--btn);
  color: var(--btn-text);
  font-weight: 600;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active { filter: brightness(1.2); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.row3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.row2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.rowlvl { display: flex; gap: 6px; }
.btn.icon { font-size: 17px; font-weight: 700; flex: none; min-width: 44px; }
.btn.level { flex: 1; min-width: 0; height: 48px; justify-content: space-between; padding: 0 10px; text-align: left; }
.rowlvl .btn.icon { height: 48px; width: 44px; }
.lvltext { display: flex; flex-direction: column; min-width: 0; }
#lvlNum { color: var(--accent-text); font-weight: 700; font-size: 14px; white-space: nowrap; }
#lvlStyle { color: var(--muted); font-weight: 400; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
[data-theme="dark"] .moon, [data-theme="light"] .sun { display: none; }

.bpm { display: flex; align-items: baseline; justify-content: center; gap: 6px; height: 28px; }
#bpm { font-weight: 800; font-size: 26px; font-variant-numeric: tabular-nums; }
.bpm .unit { color: var(--muted); font-size: 13px; font-weight: 500; }

.btn.toggle {
  height: 52px;
  flex-direction: column;
  gap: 2px;
  background: var(--panel);
  border: 1px solid var(--toggle-off-border);
  color: var(--muted);
}
.btn.toggle span { font-size: 10px; font-weight: 500; white-space: nowrap; letter-spacing: -0.2px; }
.btn.toggle[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-bg); color: var(--accent-text); }

.btn.play {
  margin-top: auto;
  height: 50px;
  flex: none;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
  font-size: 16px;
}
.btn.play .i-stop, .btn.play.playing .i-play { display: none; }
.btn.play.playing .i-stop { display: block; }

/* ---- overlays ---- */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  z-index: 10;
}
.overlay[hidden] { display: none; }
.sheet {
  background: var(--panel);
  color: var(--text);
  border-radius: 16px;
  width: min(840px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 12px 16px 16px;
}
.sheet-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 20px; flex: 1; }
.sheet-head .hint { color: var(--muted); font-size: 13px; }
.tag { font-size: 14px; font-weight: 500; color: var(--muted); margin-left: 6px; }

.seg { display: flex; gap: 4px; background: var(--btn); border-radius: 12px; padding: 3px; }
.seg .btn { height: 38px; padding: 0 16px; background: transparent; }
.seg .btn[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

.levels { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.levels .btn { height: 52px; justify-content: flex-start; padding: 0 12px; text-align: left; font-weight: 500; font-size: 14px; background: var(--card); }
.levels .btn b { color: var(--accent-text); min-width: 22px; }
.levels .btn[aria-pressed="true"] { background: var(--accent-bg); outline: 1px solid var(--accent); }

.keygrid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
.keyitem { height: auto; padding: 6px 4px 8px; flex-direction: column; gap: 2px; background: var(--card); color: var(--ink); }
.keyitem .notation { width: 100%; height: auto; max-height: 104px; }
.keyitem span { color: var(--text); font-size: 12px; font-weight: 500; }

.cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.card { background: var(--card); border-radius: 12px; padding: 12px 14px; }
.card h3 { margin: 0 0 4px; font-size: 15px; color: var(--accent-text); }
.card p { margin: 0; font-size: 14px; line-height: 1.4; color: var(--text); opacity: 0.85; }

.help { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; }
.helptext p { margin: 0 0 10px; font-size: 14px; line-height: 1.45; }
.calib { background: var(--card); border-radius: 12px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.calib h3 { margin: 0; font-size: 15px; color: var(--accent-text); }
.calib p { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.4; }
.delayrow { display: grid; grid-template-columns: 64px 1fr 64px; align-items: center; gap: 8px; }
#delayVal { text-align: center; font-weight: 700; font-size: 18px; font-variant-numeric: tabular-nums; }
.btn.wide { width: 100%; }
.tapbtn { width: 100%; height: 80px; background: var(--accent); color: var(--on-accent); font-size: 17px; }
#tapInfo { text-align: center; font-size: 13px; color: var(--muted); margin-top: 6px; }

/* ---- portrait hint ---- */
#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  color: var(--text);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 600;
}
@media (orientation: portrait) and (max-width: 900px) {
  #rotate { display: flex; }
}
@media (max-height: 360px) {
  #panel { gap: 6px; padding: 8px 10px; }
  .btn.toggle { height: 46px; }
  .bpm { height: 24px; }
  #bpm { font-size: 22px; }
}
