/*
 * The sheet (js/sheet.js): the one panel that rises above the bottom bar on
 * a phone. Linked from index.html under the same media query as
 * css/mobile.css, so a desktop browser never applies it. js/sheet.js builds
 * the markup this file styles and sets the body classes it reads:
 * sheet-open, sheet-modes or sheet-settings, sheet-half or sheet-full, and
 * sheet-dragging while a finger holds the grip.
 *
 * The sheet is fixed above the bar. At half its height is --sheet-half, a
 * px value the deck writes on body (the room under the near plate, or under
 * the board); at full its top is 12px under the safe area and its height is
 * auto, so a change of detent snaps (auto does not animate) while opening
 * and closing slide. The head is static above the body, which is the one
 * scroller, so every sticky title inside it sits at top: 0. The scrim is a
 * tap target: a 40% tint at full, transparent at half, so the board the half
 * sheet leaves visible is really visible. Z-order: bar 50 > sheet 40 >
 * scrim 39.
 *
 * Inside the sheet the mode's panel keeps its id (#panel), so its own rules
 * still reach it; what changes here is the size of things a finger uses:
 * 44px controls, 16px fields (no iOS zoom), sliders stacked under their
 * label at full width. Every sizing rule has a #sheet #panel twin (two ids)
 * so a #panel rule anywhere else (css/style.css, a per-mode sheet) loses on
 * specificity, not on link order. The shell's two groups (Board, App) that
 * js/mobile.js builds use .sheet-group, .sheet-group-title, .sheet-row,
 * .sheet-segments, .sheet-flip and .sheet-clear; an emptied drawer says
 * where its contents went.
 */

/* ---------- The sheet ---------- */

#sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--bar-h) + var(--safe-b));
  z-index: 40;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  /* The vh line is for an engine without dvh (Safari before 15.4): it keeps a height cap at all. */
  max-height: calc(100vh - var(--bar-h) - var(--safe-b) - var(--safe-t, 0px) - 12px);
  max-height: calc(100dvh - var(--bar-h) - var(--safe-b) - var(--safe-t, 0px) - 12px);
  margin: 0;
  padding: 0;
  border-radius: 14px 14px 0 0;
  background: var(--panel);
  box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.45);
  transform: translateY(calc(100% + 12px));
  visibility: hidden;
  transition: transform 0.25s ease, visibility 0s linear 0.25s;
}

/* Open: slid up; only the transform animates (full is height: auto, which no transition can interpolate, so a detent change snaps). */
body.sheet-open #sheet {
  transform: translateY(var(--sheet-drag, 0px));
  visibility: visible;
  transition: transform 0.25s ease, visibility 0s;
}

/* Under a finger the sheet follows it, with no easing in the way. */
body.sheet-dragging #sheet {
  transition: none;
}

/* Half: the height the deck measured; full: the top 12px under the safe area. */
body.sheet-half #sheet {
  height: var(--sheet-half);
}

body.staged.sheet-half #sheet {
  height: 50vh;
  height: 50dvh;
}

body.sheet-full #sheet {
  top: calc(var(--safe-t, 0px) + 12px);
  height: auto;
}

/* The page never scrolls behind an open sheet. */
body.sheet-open {
  overflow: hidden;
}

/* ---------- The scrim ---------- */

/* A tap target only (aria-hidden): Done and Escape are the accessible dismissals. */
.sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: 39;
  display: none;
  background: rgba(0, 0, 0, 0.4);
}

body.sheet-open .sheet-scrim {
  display: block;
}

body.sheet-full .sheet-scrim {
  background: rgba(0, 0, 0, 0.4);
}

/* At half the board above the sheet is the thing a think time is judged against: undimmed. */
body.sheet-half .sheet-scrim {
  background: transparent;
}

/* ---------- The grip ---------- */

/* A 40x4 pill in a 24px drag strip; the head below it takes the same drag (js/sheet.js), so the target is the two together. */
.sheet-grip {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  height: 24px;
  cursor: grab;
  touch-action: none;
}

.sheet-grip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  border-radius: 14px 14px 0 0;
}

.sheet-grip-pill {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
}

body.sheet-dragging .sheet-grip-pill {
  background: var(--accent);
}

/* ---------- The head: static above the scroller ---------- */

#sheet > .sheet-head {
  position: static;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex: none;
  margin: 0;
  padding: 0 14px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  /* A finger on the head drags the sheet like the grip (js/sheet.js). */
  touch-action: none;
}

#sheet > .sheet-head > .sheet-title {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
  font-weight: 700;
}

#sheet > .sheet-head > .sheet-close {
  flex: none;
  min-height: 44px;
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--accent);
  border-color: var(--accent);
  color: #101318;
  font-size: 16px;
  font-weight: 600;
}

/* ---------- The body: the one scroller ---------- */

.sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  /* No top padding: a sticky title's top: 0 is the scroller's content edge,
     so padding here would leave a band the content scrolls through above
     every stuck title, and scrollTo() would land a drawer 8px low. The
     slots carry the 8px instead. */
  padding: 0 14px 12px;
}

/* The two slots; js/sheet.js hides whichever kind is not open. Scoped to
   the sheet because body carries sheet-settings as a state class while
   Settings is open: a bare .sheet-settings here gave the body this padding
   and gap too, and main (and the deck's strip with it) jumped 8px down
   every time the sheet opened. */
#sheet .sheet-picker,
#sheet .sheet-settings {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 8px;
}

/* The mode's panel inside the settings slot: no box of its own, the sheet is the box. */
#sheet #panel {
  position: static;
  flex: none;
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: visible;
  border-radius: 0;
  box-shadow: none;
  transform: none;
  background: transparent;
  font-size: 16px;
  line-height: 1.35;
}

/* ---------- Sticky titles, all at top: 0 (the head is outside the scroller) ---------- */

#sheet .drawer > .drawer-title,
#sheet .sheet-group-title {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel);
}

/* The #sheet #panel twins outweigh mobile.css's 32px #panel drawer title whatever the link order. */
#sheet .drawer > .drawer-title,
#sheet #panel .drawer > .drawer-title {
  min-height: 44px;
  padding: 6px 10px;
  font-size: 0.8rem;
}

#sheet .drawer > .drawer-body,
#sheet #panel .drawer > .drawer-body {
  gap: 8px;
  padding: 0 10px 10px;
}

/* The shell's Analysis drawer keeps css/style.css's inline row on a desktop; in the sheet its rows stack like every other. */
#sheet #analysis-bar > .drawer-body {
  flex-direction: column;
  align-items: stretch;
}

/* A drawer the deck emptied (anchors are hidden, so nothing shows) says where its contents went. */
@supports selector(:has(*)) {
  #sheet .drawer > .drawer-body:not(:has(> :not([hidden])))::after {
    content: "Shown under the board.";
    color: var(--muted);
    font-size: 0.85rem;
  }
}

/* ---------- The shell's groups: Board and App (js/mobile.js) ---------- */

.sheet-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sheet-group-title {
  margin: 0;
  padding: 6px 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* The mode's hint, when it is in the sheet (the workbench face): a muted first line. */
#sheet #panel > #hint {
  margin: 0;
  color: var(--muted);
}

/* A labelled row: the words at the left, the control at the right. */
.sheet-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}

.sheet-row-label {
  font-size: 0.95rem;
}

/* Flip the board: one full-width button where the side only turns the board. */
.sheet-flip {
  width: 100%;
}

/* The App group's moved controls: the sound pack and Install at full width, Make it yours as it is. */
.sheet-group-app .pref-sound-pack,
.sheet-group-app #install-btn {
  width: 100%;
}

/* Play as: a two-way segmented control; the pressed side is filled. */
.sheet-segments {
  display: flex;
  gap: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}

.sheet-segments > button {
  flex: 1 1 0;
  min-height: 44px;
  border: 0;
  border-radius: 0;
  background: transparent;
}

.sheet-segments > button[aria-pressed="true"] {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* Clear board, armed: "Clear the board?" for three seconds, in a warmer tint. */
.sheet-clear[data-armed] {
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* ---------- Controls a finger uses: 44px tall, 16px in fields (no iOS zoom) ---------- */

#sheet select,
#sheet button,
#sheet textarea,
#sheet input[type="text"],
#sheet input[type="number"],
#sheet input[type="search"],
#sheet #panel select,
#sheet #panel button,
#sheet #panel input[type="text"],
#sheet #panel input[type="number"] {
  min-height: 44px;
  padding-top: 6px;
  padding-bottom: 6px;
  font-size: 16px;
}

#sheet input[type="checkbox"],
#sheet input[type="radio"],
#sheet #panel input[type="checkbox"],
#sheet #panel input[type="radio"] {
  width: 22px;
  height: 22px;
}

#sheet .field,
#sheet #panel .field {
  gap: 4px;
  font-size: 0.95rem;
}

#sheet .field.inline,
#sheet #panel .field.inline {
  min-height: 44px;
  gap: 10px;
}

/* ---------- Sliders: stacked under their label, full width, a 28px thumb ---------- */

#sheet .field:has(> input[type="range"]),
#sheet #panel .field:has(> input[type="range"]) {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
}

#sheet input[type="range"],
#sheet #panel input[type="range"] {
  width: 100%;
  min-height: 44px;
  height: 44px;
  margin: 0;
}

#sheet input[type="range"]::-webkit-slider-thumb,
#sheet #panel input[type="range"]::-webkit-slider-thumb {
  width: 28px;
  height: 28px;
  margin-top: -11px;
}

#sheet input[type="range"]::-moz-range-thumb,
#sheet #panel input[type="range"]::-moz-range-thumb {
  width: 28px;
  height: 28px;
}

/* ---------- Steppers: [-] slider [+] on one row, the readout tappable ---------- */

/*
 * js/sheet.js decorates every range in the sheet once (data-stepped) with a
 * [-] and a [+] button after it in the DOM; the field becomes a grid so the
 * words span the top and the buttons flank the slider below them. The
 * :has() guard keeps the rule at the stacked-slider rule's specificity (and
 * above it, by order); an engine without :has keeps the plain stacked field
 * with the two buttons under it, every one of them still working.
 */
#sheet .field:has(> input[type="range"][data-stepped]),
#sheet #panel .field:has(> input[type="range"][data-stepped]) {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  grid-template-rows: auto 44px;
  column-gap: 6px;
  row-gap: 2px;
  align-items: center;
}

/* The words take a whole row above; an engine parameter's help (a small after the range) a whole row below. */
#sheet .field:has(> input[type="range"][data-stepped]) > :not(input[type="range"]):not(.sheet-step),
#sheet #panel .field:has(> input[type="range"][data-stepped]) > :not(input[type="range"]):not(.sheet-step) {
  grid-column: 1 / -1;
}

#sheet .field:has(> input[type="range"][data-stepped]) > input[type="range"],
#sheet #panel .field:has(> input[type="range"][data-stepped]) > input[type="range"] {
  grid-column: 2;
  grid-row: 2;
}

#sheet .sheet-step,
#sheet #panel .sheet-step {
  grid-row: 2;
  width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 24px;
  line-height: 1;
  font-weight: 600;
  border-radius: 10px;
}

#sheet .sheet-step-down {
  grid-column: 1;
}

#sheet .sheet-step-up {
  grid-column: 3;
}

/*
 * The readout is a button in all but looks: a dotted underline says it can
 * be tapped. Its hit box is 44px tall (the padding, pulled back by the
 * margin so the words do not move), so a thumb aiming for "1.0 s" gets the
 * box and not the slider's thumb 3px under it.
 */
#sheet .field > span > b.value[role="button"],
#sheet #panel .field > span > b.value[role="button"] {
  display: inline-block;
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
  padding: 10px 8px;
  margin: -10px -8px;
  line-height: 24px;
  border-radius: 6px;
}

#sheet .field > span > b.value[role="button"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/*
 * The exact-value box the readout turns into: 16px (no iOS zoom), as wide as
 * a value, and no taller than the words row it stands in (28px less the
 * 4px it pulls in on each side), so the slider and its buttons never jump
 * under the finger that just tapped the readout.
 */
#sheet input.sheet-exact,
#sheet #panel input.sheet-exact {
  width: 6.5em;
  height: 28px;
  min-height: 0;
  margin: -4px 0;
  padding: 0 6px;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}

/* The readout's unit after the box ("s"), so the number means what the readout said. */
#sheet .sheet-exact-unit {
  margin-left: 4px;
  font-weight: 600;
  color: var(--accent);
}

/*
 * The sheet is one column: Bot vs Bot's White and Black side boxes stack in
 * it on every phone (css/mobile-workbench.css lets them sit side by side
 * from 400px, which leaves a stepped slider about 60px of track between its
 * two 44px buttons). Spec 7.2 draws White full width and Black below it.
 */
#sheet #viz-controls:has(> .side-box) > .side-box {
  grid-column: 1 / -1;
}

/* ---------- Short landscape: no detents, no scrim, the sheet docked beside the board ---------- */

@media (max-height: 520px) and (orientation: landscape) {
  .sheet-scrim,
  body.sheet-open .sheet-scrim {
    display: none;
  }

  /* Settings slides in from the right over the deck column; the board stays visible and live. */
  #sheet {
    top: 0;
    bottom: 0;
    /* Anchored by both edges, never a width from the viewport: the left edge is main's left inset (a notch's), the board (--board-max, inherited from body) and its 8px gutter, so the sheet covers the column and the 8px under the rail and no file of the board on a notched phone in either rotation. */
    left: calc(max(8px, var(--safe-l, 0px)) + var(--board-max, 50vw) + 8px);
    right: calc(56px + var(--safe-r, 0px));
    width: auto;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    border-radius: 12px 0 0 12px;
    transform: translateX(calc(100% + 12px));
  }

  body.sheet-open #sheet {
    transform: none;
  }

  /* A stage mode has no board column (fitBoard removes --board-max): Settings takes a fixed share beside the stage, the ladder or the dashboard still in view to its left. */
  body.staged #sheet {
    left: auto;
    width: min(420px, 60vw);
  }

  /* Modes is navigation: it covers the screen up to the rail, which stays (and closes it); a notch's inset keeps the first column of tiles out from under it. */
  body.sheet-modes #sheet,
  body.staged.sheet-modes #sheet {
    left: 0;
    width: auto;
    padding-left: var(--safe-l, 0px);
    border-radius: 0;
  }

  /* The last control clears the home indicator. */
  .sheet-body {
    padding-bottom: calc(12px + var(--safe-b, 0px));
  }

  .sheet-grip {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  #sheet,
  body.sheet-open #sheet {
    transition: none;
  }
}
