/* ── Reset & tokens ─────────────────────────────────── */
:root {
  --bg:       #07070d;
  --panel:    #11111b;
  --panel2:   #191925;
  --border:   #2a2a3a;
  --text:     #f4f4fb;
  --muted:    #818193;
  --red:      #e63946;
  --green:    #2a9d8f;
  --blue:     #4cc9f0;
  --yellow:   #e9c46a;
  --pink:     #f72585;
  --realVH:   100vh;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0; padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100vw;
  height: var(--realVH);
  max-width: 100vw;
  max-height: var(--realVH);
  overflow: hidden;
  position: fixed;
  inset: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', monospace;
}

button, input, select, textarea {
  font-family: inherit;
}

/* ── App shell ──────────────────────────────────────── */
#app, #app * { box-sizing: border-box; min-width: 0; }  /* nothing inside may force the page wider than the phone */

#app {
  width: 100vw;
  height: var(--realVH);
  max-width: 720px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto auto minmax(0,1fr) auto;
  gap: 5px;
  padding: 5px;
  overflow: hidden;
}

/* ── Header ─────────────────────────────────────────── */
#header {
  text-align: center;
  padding: 2px 0;
}
.brand {
  font-size: 8px;
  letter-spacing: 3px;
  color: #555;
}
.app-title {
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 1px;
}
.status {
  font-size: 10px;
  color: #858598;
  min-height: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}
.status-flash {
  animation: statusFlash 0.7s ease-out;
}
@keyframes statusFlash {
  0%   { color: #fff; }
  100% { color: #858598; }
}

/* ── Transport ───────────────────────────────────────── */
#transport {
  background: linear-gradient(180deg, #11111b, #0b0b13);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px;
}
.bpm-row {
  display: grid;
  grid-template-columns: 34px 56px 34px minmax(0,1fr) 44px;
  gap: 5px;
  align-items: center;
}
.btn-adj {
  padding: 10px 0;
  font-size: 16px;
  line-height: 1;
}
.bpm-display {
  cursor: pointer;
  background: #ffffff08;
  border: 1px solid #ffffff14;
  border-radius: 10px;
  padding: 3px 2px;
}
.bpm-display:active { background: #ffffff18; }
.bpm-num {
  font-size: 30px;
  font-weight: 900;
  text-align: center;
  line-height: 1;
}
.bpm-tag {
  font-size: 7px;
  color: #666;
  letter-spacing: 2px;
  text-align: center;
}
.bpm-slider {
  width: 100%;
  accent-color: var(--red);
  cursor: pointer;
}
.play-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 5px;
  margin-top: 6px;
}

/* ── Buttons ─────────────────────────────────────────── */
.btn {
  border: 0;
  border-radius: 12px;
  color: #fff;
  padding: 9px 5px;
  font-size: 10px;
  font-weight: 900;
  cursor: pointer;
  background: var(--panel2);
}
.btn-ghost  { background: #ffffff12; color: #ccc; }
.btn-play   { background: var(--green); box-shadow: 0 0 12px #2a9d8f44; }
.btn-stop   { background: var(--red);   box-shadow: 0 0 12px #e6394644; }
.btn-danger { background: #7b202a; }
.btn-primary{ background: var(--green); }
.btn-nav    { padding: 8px 12px; }
.btn-loop-active {
  background: var(--yellow) !important;
  color: #07070d !important;
  box-shadow: 0 0 10px #e9c46a66;
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Student mode: hide edit controls */
body.student-mode #bottomBar .btn-edit-only { display: none; }
body.student-mode #stageHeader .btn-nav     { display: none; }

/* ── Stage ───────────────────────────────────────────── */
#beatPulse {
  height: 4px;
  width: 100%;
  background: #1a1a28;
  border-radius: 0 0 4px 4px;
  transition: background .08s;
  flex-shrink: 0;
}
#beatPulse.pulse        { background: #e9c46a99; }
#beatPulse.pulse-strong { background: #fff; box-shadow: 0 0 12px #fff8; }

#stage {
  background: #090911;
  border: 1px solid #242438;
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
#stageHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  border-bottom: 1px solid #ffffff10;
  flex-shrink: 0;
}
.stage-name {
  font-size: 10px;
  font-weight: 900;
  text-align: center;
  max-width: 58vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.stage-type {
  font-size: 8px;
  color: #aaa;
  background: #ffffff10;
  border-radius: 999px;
  padding: 3px 7px;
  text-align: center;
  margin-top: 2px;
}
#stageBody {
  flex: 1;
  min-height: 0;
  overflow-y: auto;          /* long generated lessons scroll here; PLAY never moves */
  overflow-x: hidden;
  padding: 4px;
  width: 100%;
  -webkit-overflow-scrolling: touch;
}

/* ── Score layout (stacked sections) ────────────────── */
.score-wrap {
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  height: auto;              /* grows with its sections; #stageBody scrolls the growth */
  display: grid;
  overflow: visible;
  gap: 6px;
  box-sizing: border-box;
}
.score-section {
  min-height: 0;
  min-width: 0;
  border: 1px solid #ffffff14;
  border-radius: 10px;
  background: #ffffff05;
  padding: 10px 4px 4px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  /* Constrain to grid cell — never push siblings off screen */
  max-width: 100%;
  box-sizing: border-box;
}
.score-section.playing {
  border-color: var(--yellow);
  box-shadow: 0 0 14px #e9c46a44;
  background: #e9c46a08;
}
.score-section-label {
  position: absolute;
  top: 2px; left: 4px; right: 4px;
  font-size: 6px;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Normal beat row */
.beat-row {
  /* Never wider than the container — dots wrap rather than overflow */
  max-width: 100%;
  width: 100%;
  height: 100%;
  display: grid;
  justify-content: center;
  align-content: center;
  justify-items: center;
  align-items: center;
  overflow: hidden;
  box-sizing: border-box;
}
.beat-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: visible;
  width: var(--dot, 24px);
  height: var(--dot, 24px);
}
.beat-dot {
  border-radius: 50%;
  border: 2px solid var(--c, #4cc9f0);
  background: color-mix(in srgb, var(--c) 25%, #20202c);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  width: var(--dot, 24px);
  height: var(--dot, 24px);
  flex-shrink: 0;
  /* font-size set inline by render.js */
}
.beat-dot.active {
  transform: scale(1.25);
  box-shadow: 0 0 0 2px #fff8, 0 0 18px var(--c);
  background: color-mix(in srgb, var(--c) 75%, #fff);
  color: #05050a;
  z-index: 5;
}
/* Muted beat — dimmed, dashed border, ✕ overlay */
.beat-dot.muted {
  opacity: 0.32;
  border-style: dashed;
  border-color: var(--c);
}
.beat-dot.accent {
  border-width: 3px;
  border-color: var(--yellow);
  box-shadow: 0 0 8px #e9c46a66;
}
/* Start point marker — glowing ring */
.beat-dot.start-point {
  box-shadow: 0 0 0 3px #fff, 0 0 10px #fff8;
}
/* Muted flash — dim pulse, not bright */
.beat-dot.active-muted {
  transform: scale(1.1);
  opacity: 0.5;
  z-index: 5;
}
.bar-line {
  position: absolute;
  width: 2px;
  /* left / top / bottom set inline by render.js (px values) */
  background: rgba(255,255,255,0.9);
  border-radius: 2px;
  pointer-events: none;
  z-index: 2;
}

/* ── Bar-line tap zone (teacher mode) ───────────────── */
.bar-tap-zone {
  position: absolute;
  /* width / left / top / bottom set inline by render.js */
  z-index: 10;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* Hint line: a 2px wide pseudo-element centred inside the tap zone.
   Uses repeating-linear-gradient for dashes — no border-left offset issue. */
.bar-tap-zone::before {
  content: '';
  display: block;
  width: 2px;
  height: 100%;
  border-radius: 2px;
  background: repeating-linear-gradient(
    to bottom,
    rgba(255,255,255,0.22) 0px,
    rgba(255,255,255,0.22) 5px,
    transparent 5px,
    transparent 9px
  );
  transition: background .12s;
}
.bar-tap-zone:hover::before {
  background: repeating-linear-gradient(
    to bottom,
    rgba(255,255,255,0.7) 0px,
    rgba(255,255,255,0.7) 5px,
    transparent 5px,
    transparent 9px
  );
}
/* When bar IS active — real bar-line div shows the solid line; hide the hint */
.bar-tap-zone.has-bar::before {
  background: transparent;
}
/* Hover on active bar = red tint warning that tapping removes it */
.bar-tap-zone.has-bar:hover::before {
  background: rgba(230,57,70,0.5);
  border-radius: 2px;
}

/* Poly row */
.poly-wrap {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 4px;
  overflow: hidden;
}
.poly-lane {
  position: relative;
  min-height: 0;
  border-top: 1px solid #ffffff18;
  border-bottom: 1px solid #ffffff10;
  overflow: hidden;
}
.poly-lane-label {
  position: absolute;
  left: 0; top: -10px;
  font-size: 6px;
  font-weight: 900;
  color: var(--c);
}
.poly-dot {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 1.5px solid var(--c);
  background: color-mix(in srgb, var(--c) 30%, #1d1d29);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  width: var(--pdot, 14px);
  height: var(--pdot, 14px);
  font-size: calc(var(--pdot, 14px) * .3);
}
.poly-dot.active {
  transform: translate(-50%, -50%) scale(1.35);
  box-shadow: 0 0 0 2px #fff8, 0 0 16px var(--c);
  background: color-mix(in srgb, var(--c) 75%, #fff);
  color: #05050a;
  z-index: 6;
}
.poly-dot.muted {
  opacity: 0.32;
  border-style: dashed;
}
.poly-dot.start-point {
  box-shadow: 0 0 0 3px #fff, 0 0 10px #fff8;
}
.poly-dot.active-muted {
  transform: translate(-50%, -50%) scale(1.1);
  opacity: 0.5;
  z-index: 6;
}
.beat-line {
  position: absolute;
  top: -4px; bottom: -4px;
  width: 2px;
  background: #e9c46a55;
}
.beat-line.strong {
  width: 3px;
  background: #fff;
  opacity: .8;
}
.align-line {
  position: absolute;
  top: -4px; bottom: -4px;
  width: 1px;
  background: #ffffff18;
}
.align-line.together { width: 3px; background: #ffffffcc; }
.align-line.top-only  { background: #f7258550; }
.align-line.bot-only  { background: #4cc9f050; }

/* ── Bottom nav ──────────────────────────────────────── */
#bottomNav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
#sectionPills {
  display: flex;
  gap: 5px;
  overflow-x: auto;
  padding: 2px 0;
  scrollbar-width: none;
}
#sectionPills::-webkit-scrollbar { display: none; }
.pill {
  white-space: nowrap;
  padding: 6px 9px;
  border-radius: 999px;
  background: #ffffff0d;
  border: 1px solid #ffffff10;
  font-size: 8px;
  color: #aaa;
  cursor: pointer;
  font-family: 'Courier New', monospace;
  font-weight: 900;
}
.pill.active { background: #ffffff22; color: #fff; border-color: #fff; }
.pill-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}
#bottomBar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}
#bottomBar .btn { padding: 9px 2px; font-size: 9px; }

/* ── Drawer ──────────────────────────────────────────── */
#drawerOverlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  background: #00000060;
}
#drawerOverlay.visible { display: block; }
#drawer {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  background: #0d0d15;
  border-top: 1px solid #303044;
  border-radius: 20px 20px 0 0;
  max-height: 88vh;
  overflow-y: auto;
  padding: 8px 12px 24px;
  transform: translateY(100%);
  transition: transform .22s ease;
}
#drawer.open { transform: translateY(0); }
#drawerHandle {
  width: 44px; height: 4px;
  background: #444;
  border-radius: 999px;
  margin: 0 auto 8px;
  cursor: pointer;
}
#drawerTitle {
  text-align: center;
  font-size: 10px;
  letter-spacing: 2px;
  color: #888;
  margin-bottom: 10px;
}

/* ── Drawer panels ───────────────────────────────────── */
.d-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 10px;
}
.d-label {
  font-size: 9px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #666;
  margin-bottom: 8px;
}
.d-grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.d-full  { grid-column: 1 / -1; }
.d-small { font-size: 10px; color: var(--muted); line-height: 1.45; }
.d-warn  { font-size: 10px; color: var(--yellow); line-height: 1.4; margin-top: 6px; }

input[type=text], input[type=number], select, textarea {
  width: 100%;
  background: #090911;
  border: 1px solid #343448;
  border-radius: 10px;
  color: var(--text);
  padding: 9px;
  font-size: 13px;
  outline: none;
  font-family: inherit;
}
textarea { height: 54px; resize: vertical; }
label { font-size: 10px; color: var(--muted); display: block; margin-bottom: 3px; }
input[type=range] { width: 100%; accent-color: var(--red); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  padding: 4px 0;
}
.check-row input[type=checkbox] {
  width: 20px;
  height: 20px;
  accent-color: var(--green);
  cursor: pointer;
}
.check-row span { flex: 1; }
.btn-row .btn { flex: 1; min-width: 80px; padding: 10px 6px; font-size: 10px; }

/* ── Voice panel ─────────────────────────────────────── */
.voice-section-title {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--yellow);
  margin: 10px 0 6px;
}
.voice-row {
  background: #ffffff08;
  border: 1px solid #ffffff10;
  border-radius: 10px;
  padding: 7px;
  margin-bottom: 6px;
}
.voice-row-top {
  display: grid;
  grid-template-columns: 28px minmax(0,1fr) auto auto auto;
  gap: 5px;
  align-items: center;
}
.voice-idx {
  font-size: 10px;
  font-weight: 900;
  color: #aaa;
  text-align: center;
}
.voice-row input[type=text] {
  padding: 7px;
  font-size: 11px;
}
.voice-row .btn {
  padding: 7px 8px;
  font-size: 9px;
  border-radius: 8px;
  white-space: nowrap;
}
.btn-rec-active {
  background: var(--red) !important;
  animation: pulseRec .6s infinite alternate;
}
@keyframes pulseRec {
  from { box-shadow: 0 0 0 #e6394600; }
  to   { box-shadow: 0 0 14px #e63946aa; }
}

/* Waveform */
.wave-wrap {
  position: relative;
  height: 36px;
  background: #07070d;
  border: 1px solid #ffffff18;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 6px;
}
.wave-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
}
.wave-beat-marker {
  position: absolute;
  top: 0; bottom: 0;
  width: 2px;
  background: rgba(255,255,255,.85);
  pointer-events: none;
}

/* Skip slider */
.skip-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px;
  align-items: center;
  margin-top: 5px;
}
.skip-row label { font-size: 9px; color: #888; margin: 0; }
.skip-val { font-size: 9px; color: var(--yellow); text-align: right; min-width: 52px; }

/* Vol row */
.vol-row {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

/* ── Save drawer ─────────────────────────────────────── */
.save-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}
.save-grid .btn { padding: 12px 8px; font-size: 11px; }
.save-divider {
  font-size: 9px;
  color: #555;
  text-align: center;
  margin: 8px 0;
}
#lessonCode, #webLink, #telegramLink {
  margin-top: 6px;
  font-size: 11px;
}

/* ── Sections drawer ─────────────────────────────────── */
.section-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}
.section-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #ffffff08;
  border: 1px solid #ffffff10;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}
.section-item.selected {
  border-color: var(--yellow);
  background: #e9c46a0d;
}
.section-item-name {
  flex: 1;
  font-size: 11px;
  font-weight: 900;
}
.section-item-type {
  font-size: 9px;
  color: #888;
  background: #ffffff10;
  border-radius: 999px;
  padding: 3px 7px;
}

/* ── Mode badge ──────────────────────────────────────── */
.mode-teacher { color: var(--yellow); }
.mode-student { color: var(--blue); }

/* ── In-app dialog (Telegram blocks native ones) ─────── */
#bezDlg { position:fixed; inset:0; z-index:200; background:rgba(0,0,0,.55);
  display:flex; align-items:center; justify-content:center; }
.bez-dlg-card { background:var(--panel,#1a1f2b); color:var(--text,#eee);
  border:1px solid var(--line,#333); border-radius:12px; padding:16px;
  width:300px; max-width:86vw; box-shadow:0 10px 40px rgba(0,0,0,.5); }
.bez-dlg-msg { font-size:13px; margin-bottom:10px; }
.bez-dlg-card input { width:100%; box-sizing:border-box; padding:8px;
  background:var(--bg,#10151b); color:var(--text,#eee);
  border:1px solid var(--line,#333); border-radius:8px; font-family:inherit; margin-bottom:10px; }
.bez-dlg-btns { display:flex; gap:8px; justify-content:flex-end; }
