/* Song view: the chart as a sheet (over the library list) or filling the page, with a fixed header and footer. Two
   views, Chart and Details (songview_details.css); the view switch below chooses between them. Loaded after ui_sweep.css and chart_reading.css because it overrides
   them at equal specificity. Colours only through tokens. */

/* Dim, do not wash out: the same dark scrim in light and dark appearance, with the list still readable behind. */
.library-entry-dialog::backdrop {
  background: color-mix(in srgb, var(--brand-charcoal) 55%, transparent);
}

/* A soft shadow lifts the sheet off the list (ui_sweep.css zeroes shadows on panels, so it is set here, after it). */
.library-entry-dialog {
  box-shadow: 0 24px 64px color-mix(in srgb, var(--brand-charcoal) 45%, transparent);
}

@media (max-width: 40rem), (max-height: 30rem) {
  .library-entry-dialog {
    box-shadow: none;
  }
}

/* ---------------------------------------------------------------------------------------------------------------
   Layout. The song is one grid: chrome (navigation on a full page, the header, the section row) / the
   chart area (the only thing that scrolls) / the footer. The same rules serve the library sheet and the full page,
   so the full page looks like the sheet filling the screen. Nothing here depends on a body class.
   --------------------------------------------------------------------------------------------------------------- */
.songview.chart-preview {
  display: grid;
  width: 100%;
  height: 100%;
  min-height: 0;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 0;
  padding: 0;
  color: var(--ink);
  background: var(--bg);
}

.performance-page .songview.chart-preview {
  height: 100dvh;
}

.songview .chart-stage__chrome {
  position: relative;
  z-index: 3;
}

.songview .chart-stage__scroller {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.songview .performance-chart {
  min-height: 100%;
}

.songview .performance-surface {
  width: min(84rem, 100%);
  padding-bottom: 1.5rem;
}

/* ---------------------------------------------------------------------------------------------------------------
   Header: close | title and artist | view switch (Chart | Details | Edit) | Share.
   --------------------------------------------------------------------------------------------------------------- */
.songview .chart-stage__header-rail {
  display: flex;
  width: 100%;
  max-width: none;
  min-height: 56px;
  align-items: center;
  gap: 12px;
  margin: 0;
  padding: 4px clamp(8px, 2vw, 20px);
}

.songview .chart-stage__side--start {
  flex: none;
}

.songview .chart-stage__identity {
  min-width: 0;
  flex: 1 1 0;
  text-align: left;
}

/* The display face and the 22px / 26px size come from chart_reading.css (never under 20px). */
.songview .chart-stage__identity h1 {
  margin: 0;
  overflow: hidden;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.songview .chart-stage__identity p {
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.songview .chart-stage__side--end {
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
}

/* Mode switch: two view segments plus an Edit link. The selected view uses the selected tokens. */
.songview-mode {
  display: inline-flex;
  flex: none;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  overflow: hidden;
  background: var(--surface);
}

.songview-mode__views {
  display: inline-flex;
}

.songview-mode__option,
.songview-mode__edit {
  display: inline-flex;
  min-width: 5.25rem;
  min-height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 0;
  color: var(--ink);
  background: transparent;
  font: 700 0.95rem/1 var(--font-ui);
  align-items: center;
  justify-content: center;
  text-decoration: none;
  white-space: nowrap;
}

.songview-mode__option {
  cursor: pointer;
}

.songview-mode__edit {
  border-left: 1px solid var(--hairline);
}

/* Selected: a teal outline with a pastel teal fill (never a solid teal block). */
.songview-mode__option[aria-pressed="true"] {
  color: var(--ink);
  background: var(--selected-fill);
  box-shadow: inset 0 0 0 2px var(--selected-line);
}

.songview-mode__option:focus-visible,
.songview-mode__edit:focus-visible {
  outline-offset: -3px;
}

/* A phone in portrait has no room for title and switch on one row: the switch gets its own full-width row inside
   the same header, in both modes. */
@media screen and (max-width: 36rem) {
  .songview .chart-stage__header-rail {
    flex-wrap: wrap;
    row-gap: 4px;
    padding-top: max(4px, env(safe-area-inset-top));
  }

  .songview .chart-stage__identity {
    order: 1;
  }

  .songview .chart-stage__side--end {
    order: 2;
  }

  .songview-mode {
    order: 3;
    flex: 1 0 100%;
    margin-bottom: 4px;
  }

  .songview-mode__views,
  .songview-mode__option,
  .songview-mode__edit {
    flex: 1 1 0;
    min-width: 0;
  }

  .songview-mode__views {
    flex-grow: 2;
  }

  .songview-mode__option,
  .songview-mode__edit {
    padding-inline: 8px;
  }
}

/* Share is icon-only in this member chart header, with a 44px hit area. */
.songview .share-sheet-launch > button,
.songview .chart-stage__share {
  width: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  justify-content: center;
}

/* Section buttons: a slim row under the header, hidden until the title is tapped. */
.songview-jumps {
  position: relative;
  display: none;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  padding: 4px clamp(8px, 2vw, 20px);
  border-bottom: 1px solid var(--rule);
  background: var(--bg);
  scrollbar-width: none;
}

.songview-jumps::-webkit-scrollbar {
  display: none;
}

.songview.chart-preview--sections-open .songview-jumps {
  display: flex;
}

.songview-jumps .chart-section-jump {
  flex: none;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--ink);
  background: var(--surface);
  font: 600 15px/1 var(--font-ui);
  white-space: nowrap;
}

.songview-jumps .chart-section-jump--current {
  border-color: var(--selected-line);
  color: var(--selected-ink);
  background: var(--selected-fill);
}

@media (pointer: coarse) {
  .songview-jumps .chart-section-jump {
    min-height: 44px;
  }
}

/* ---------------------------------------------------------------------------------------------------------------
   Footer: Key, Aa, then the scroll group (delay, slower / speed / faster, Start-Pause). One row, 56px; every target
   at least 44px with 8px between neighbours; the big button is last, under the thumb.
   --------------------------------------------------------------------------------------------------------------- */
.songview .performance-controls {
  position: relative;
  z-index: 4;
  inset: auto;
  width: auto;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  border: 0;
  border-top: 1px solid var(--rule);
  background: var(--surface);
}

.songview .performance-controls__rail {
  display: flex;
  width: 100%;
  min-height: 56px;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 6px clamp(8px, 2vw, 20px);
}

.songview .performance-control {
  position: static;
  flex: none;
}

.songview .performance-control__trigger {
  width: auto;
  min-width: 4.25rem;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--hairline);
  background: var(--surface);
  white-space: nowrap;
}

.songview .performance-control__trigger:hover,
.songview .performance-control__trigger[aria-expanded="true"] {
  border-color: var(--hairline);
  background: var(--tint);
}

/* Key and Aa sheets open upward from the footer, inside the song. */
.songview .performance-control__overlay {
  position: absolute;
  z-index: 6;
  top: auto;
  right: 8px;
  bottom: calc(100% + 6px);
  left: 8px;
  width: auto;
  max-width: 40rem;
  margin: 0 auto;
}

/* Auto scroll: one button, the largest target, at the right edge (the thumb side). The speeds rise from it. */
.songview-autoscroll {
  position: relative;
  margin-left: auto;
}

.songview-go {
  display: inline-flex;
  min-width: 9.5rem;
  min-height: 48px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 18px;
  border: 1px solid var(--selected-line);
  border-radius: var(--radius-control);
  color: var(--selected-ink);
  background: var(--selected-fill);
  font: 700 1.05rem/1 var(--font-ui);
  white-space: nowrap;
  cursor: pointer;
}

.songview-go__icon {
  display: inline-grid;
  width: 1.25rem;
  height: 1.25rem;
  place-items: center;
}

.songview-go__icon > * {
  grid-area: 1 / 1;
}

.songview-go__icon .icon {
  width: 1.25rem;
  height: 1.25rem;
  fill: currentColor;
}

/* Stop glyph (a filled square) while the lead-in or scrolling is on. */
.songview-go__stop {
  display: none;
  width: 0.95rem;
  height: 0.95rem;
  border-radius: 2px;
  background: currentColor;
}

.songview-go[data-state="active"] .icon,
.songview-go[data-state="counting"] .icon {
  display: none;
}

.songview-go[data-state="active"] .songview-go__stop,
.songview-go[data-state="counting"] .songview-go__stop {
  display: block;
}

/* The five speeds, directly above the button and no wider than it needs: 5 x 44px targets. They stay for as long as
   scrolling (or the lead-in) is on; the button underneath still stops it. */
.songview-speeds {
  position: absolute;
  z-index: 6;
  right: 0;
  bottom: calc(100% + 6px);
  display: flex;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-card);
  background: var(--surface);
  box-shadow: var(--shadow-floating);
}

.songview-speeds[hidden] {
  display: none;
}

.songview-speed {
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-control);
  color: var(--ink);
  background: var(--surface);
  font: 700 1rem/1 var(--font-ui);
  cursor: pointer;
}

.songview-speed[aria-pressed="true"] {
  border-color: var(--selected-line);
  color: var(--selected-ink);
  background: var(--selected-fill);
}

/* The 5 second lead-in: a slim pill over the top edge of the chart area. It sits in the chart's grid cell, so
   nothing moves when it appears or goes. */
.songview-lead {
  z-index: 5;
  grid-column: 1;
  grid-row: 2;
  align-self: start;
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  padding: 2px 2px 2px 14px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow-floating);
  font: 600 0.95rem/1 var(--font-ui);
}

.songview-lead[hidden] {
  display: none;
}

.songview-lead__text b {
  display: inline-block;
  min-width: 1ch;
  text-align: center;
}

.songview-lead__skip {
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--selected-line);
  border-radius: var(--radius-pill);
  color: var(--selected-ink);
  background: var(--selected-fill);
  font: 700 0.9rem/1 var(--font-ui);
  cursor: pointer;
}

.songview .chart-stage__chrome { grid-row: 1; }
.songview .chart-stage__scroller { grid-row: 2; grid-column: 1; }
.songview .performance-controls { grid-row: 3; grid-column: 1; }

/* Phone: the footer stays one row, Key | Aa | Auto scroll. */
@media screen and (max-width: 40rem) {
  .songview .performance-controls__rail {
    gap: 6px;
    padding-inline: 8px;
  }

  .songview .performance-control__trigger {
    min-width: 3.5rem;
    padding: 0 10px;
  }

  .songview-go {
    min-width: 8.5rem;
    padding: 0 12px;
  }
}

/* A phone on its side: header and footer shrink to 48px (targets stay 44px) so the chart keeps most of the height. */
@media screen and (max-height: 30rem) {
  .songview .chart-stage__header-rail,
  .songview .performance-controls__rail {
    min-height: 48px;
    padding-block: 2px;
  }

  .songview .chart-stage__header,
  .songview .chart-stage__header-rail {
    min-height: 48px;
  }

  .songview-go {
    min-height: 44px;
  }

  .songview-lead {
    margin-top: 2px;
  }

  .songview-jumps {
    padding-block: 2px;
  }

  .songview-jumps .chart-section-jump {
    min-height: 40px;
  }
}

/* ---------------------------------------------------------------------------------------------------------------
   Text size (Aa). The saved preference is still the step, 1 to 5 (--chart-font-step, set beside --chart-font-size by the
   view and by performance_controller.js). Each device class has a lyric size RANGE, and the five steps are spread evenly
   across it, so every step is visibly different on every device:
       size = min + (step - 1) / 4 x (max - min)
   Default (step 3) is the middle of the range: 19px on a phone, 22px on a tablet, 23px on a wide screen (the round-4
   verdict asked for 19px and 22px). This replaces round 4's fixed floor max(22px, ...), on the owner's instruction.
     phones   (< 600px)        14 - 24px:  14   16.5  19    21.5  24
     tablets  (600 - 1099px)   16 - 28px:  16   19    22    25    28
     wide     (>= 1100px)      16 - 30px:  16   19.5  23    26.5  30
   Auto (the resting position, stored as 0) is one of steps 2 to 4, whichever is the largest at which the longest line fits.
   Chords stay 0.8em of the lyric and the line height is in em, so both scale with it. Stage and print are separate.
   --------------------------------------------------------------------------------------------------------------- */
.songview .performance-chart {
  --songview-size-min: 14px;
  --songview-size-max: 24px;
}

@media (min-width: 37.5rem) {
  .songview .performance-chart {
    --songview-size-min: 16px;
    --songview-size-max: 28px;
  }
}

@media (min-width: 68.75rem) {
  .songview .performance-chart {
    --songview-size-min: 16px;
    --songview-size-max: 30px;
  }
}

@media screen {
  .songview .chart-sheet {
    font-size: calc(var(--songview-size-min) + (var(--chart-font-step, 3) - 1) / 4 * (var(--songview-size-max) - var(--songview-size-min)));
  }
}

/* ---------------------------------------------------------------------------------------------------------------
   Fit. The chart never scrolls sideways. Aa rests on "Auto" (performance_controller.js picks the step; floor step 2, cap
   step 4), and a line that still does not fit WRAPS, only between words (a word is a whole chord-and-lyric group: chart_units.css
   keeps each word in one box, so a chord is never parted from its syllable and a word is never split).
   The continuation row is hung: indented by --wrap-indent, with a hairline in the indent from the first wrapped row down
   (data-wrapped and --wrap-from are set by markWrappedLines), so a wrapped line reads as one line and the next line starts
   flush left again. Chord-only lines wrap the same way, between chords. Print and Stage are separate.
   --------------------------------------------------------------------------------------------------------------- */
.songview .performance-chart {
  --wrap-indent: 1.6em;
}

/* A size change lands at once: the fit measures the finished size, and the wraps and cues are drawn for it (chart.css eases
   the font size over 120ms, which would be measured half way). */
.songview .chart-sheet {
  transition: none;
}

/* The reading column is 46em; Auto widens it (--chart-fit-width) only as far as the longest line needs. */
.songview .chart-document--units {
  max-width: max(46em, var(--chart-fit-width, 0px));
}

.songview .chart-line--units {
  padding-left: var(--wrap-indent);
  text-indent: calc(-1 * var(--wrap-indent));
}

.songview .chart-line--units .chart-word {
  text-indent: 0;
}

.songview .chart-line--units[data-wrapped] {
  position: relative;
}

.songview .chart-line--units[data-wrapped]::before {
  content: "";
  position: absolute;
  top: var(--wrap-from, 1.6em);
  bottom: 0.25em;
  left: calc(var(--wrap-indent) * 0.35);
  width: 2px;
  border-radius: 1px;
  background: color-mix(in srgb, var(--ink) 22%, transparent);
  pointer-events: none;
}

/* A unit is never narrower than its chord. The chord is drawn above its syllable and takes no width, so a chord wider than a
   short word ("Bm7" over "of") would hang past the end of the word, and past the edge of the sheet when that word ends a row.
   The unit measures the chord itself with a hidden, zero-height copy of its text (data-chord-text), the same trick the
   chord-only units use (chart_units.css); the copy is generated content, so it is not selected, copied or read. */
.songview .chart-line--units-pair .chart-unit[data-chord-text]::before {
  content: attr(data-chord-text);
  display: block;
  height: 0;
  overflow: hidden;
  visibility: hidden;
  font-family: var(--font-chord);
  font-size: var(--chart-chord-size, 0.78em);
  font-weight: var(--chart-chord-weight, 750);
  line-height: 1.2;
  white-space: pre;
}

/* Natural width of a line, measured off the layout in one task (never painted): no wrapping, no hanging indent. */
.songview .chart-document--measuring .chart-line--units,
.songview .chart-document--measuring .chart-line--tab {
  width: max-content;
  max-width: none;
  min-width: 0;
  padding-left: 0;
  text-indent: 0;
  white-space: nowrap;
  overflow: visible;
}

/* Tab blocks cannot wrap: they shrink (--chart-tab-size, never below 12px) to the sheet, and one that still does not fit
   scrolls as ONE box (all its strings together) with soft edges that show there is more. */
.songview .chart-tab-block {
  max-width: 100%;
  overflow-x: auto;
}

.songview .chart-line--tab {
  width: max-content;
  min-width: 100%;
  max-width: none;
  overflow: visible;
  font-size: var(--chart-tab-size, 1em);
}

.songview .chart-tab-block[data-overflowing] {
  background:
    linear-gradient(to right, var(--surface), transparent) left center / 1.5em 100% local no-repeat,
    linear-gradient(to left, var(--surface), transparent) right center / 1.5em 100% local no-repeat,
    linear-gradient(to right, color-mix(in srgb, var(--ink) 16%, transparent), transparent) left center / 0.6em 100% scroll no-repeat,
    linear-gradient(to left, color-mix(in srgb, var(--ink) 16%, transparent), transparent) right center / 0.6em 100% scroll no-repeat;
}

/* Nothing in the chart can be scrolled sideways, not even by focus or a script: clip, not hidden (a chord's trailing
   annotation spaces, which are invisible, reach past the edge and would make a hidden box scrollable). */
.songview .chart-sheet--units {
  overflow-x: hidden;
  overflow-x: clip;
}

/* The width must not change when the page gains a scrollbar, or Auto would refit and flip. */
.songview .chart-stage__scroller {
  scrollbar-gutter: stable;
}

/* A section picked from the jump row lands just under the top edge. */
.songview .chart-line--section {
  scroll-margin-top: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .songview-jumps {
    scroll-behavior: auto;
  }
}

/* ---------------------------------------------------------------------------------------------------------------
   Print: the song prints as one flowing page, as before.
   --------------------------------------------------------------------------------------------------------------- */
@media print {
  .songview.chart-preview,
  .performance-page .songview.chart-preview {
    display: block;
    height: auto;
  }

  .songview .chart-stage__scroller {
    overflow: visible;
  }

  .songview-mode,
  .songview-jumps,
  .songview .performance-controls {
    display: none !important;
  }
}

/* The full chart page (a deep link or reload) has no member bar: the sheet header is the top of the page, exactly as in the
   library overlay. The older rule that slid the chrome up by the bar's height left a 60px empty band under the Chart | Details
   switch, because this layout is a fixed-height grid rather than a scrolling page. */
.member-shell--chart.chart-reading-page .songview .chart-stage__chrome {
  top: 0;
}

.member-shell--chart.chart-reading-page .songview .chart-stage__chrome > .member-header {
  display: none;
}
