/* Stingray Bookkeeping, pass 3: the "Customize this page" panel (customize-pass3.js). Shared by the client pages
   and the owner screens. Pictures: chatgpt-pass6 O1 (side panel), O2 (phone bottom sheet), O3 (what a client sees
   first), with the build fixes of README-customize.md. Rule: INTEGRATION-CONTRACT.md section 8.

   Load it after the page's own css (client pages: after client-pass3.css; owner: after owner-aurora.css).
   The sheet carries the shared "panel" class too: each look's surface words come with it (engraved cream turns
   its surfaces cream with navy words); every drawn property of the sheet is set here, after portal.css.
   Every rule starts with .sb-customize. No color, no font name, no size token of its own: only the Look A names of
   INTEGRATION-CONTRACT.md section 1.2 (var(--...)), so it draws right in all four looks. The fonts follow the role
   names (--font-head, --font-ui, --font-body), so each look decides serif or sans, as on every other page button.
   The only other color words are the system colors of Windows high contrast (forced colors), at the end.
   Side panel from 900px wide, bottom sheet below (contract breakpoints 899 / 699). Text 16px or more on a computer,
   18px or more up to 699px wide. Everything pressed is 48px tall or more, with words. 200 ms movement, none when the
   person asks for less movement. The focus ring is the shared one (portal.css :focus-visible), never restyled
   (only its color in high contrast, where the switch draws its own colors). Sizes that follow the text are in rem,
   so a larger browser font size puts each row on two lines instead of squeezing the names. */

/* ------------------------------------------------------------------ the layer */
.sb-customize[hidden] {
  display: none;
}

.sb-customize {
  position: fixed;
  inset: 0;
  z-index: 400;                      /* over the owner top bar (200) and the phone bar */
  pointer-events: none;
  color: var(--ink);
  font-family: var(--font-body);
}

.sb-customize .sb-customize-scrim {
  position: absolute;
  inset: 0;
  background: var(--shade);
  opacity: 0;
  pointer-events: auto;
  transition: opacity 200ms ease-out;
}

/* a computer keeps the page readable behind (the live view is the point); a phone dims it fully */
.sb-customize[data-state="open"] .sb-customize-scrim {
  opacity: .62;
}

/* ------------------------------------------------------------------ the side panel (computer) */
.sb-customize .sb-customize-sheet {
  position: absolute;
  top: 16px;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  width: min(680px, calc(100vw - 280px));
  padding: 0;
  isolation: isolate;
  border: 1px solid var(--btn2-line);
  border-radius: var(--panel-radius);
  background: var(--pop-bg);
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  box-shadow: var(--panel-shadow);
  color: var(--ink);
  pointer-events: auto;
  overflow: hidden;
  transform: translateX(calc(100% + 24px));
  transition: transform 200ms cubic-bezier(.2, .7, .2, 1);
  container-type: inline-size;
  container-name: sbc;
}

/* the page narrows beside the panel (from 1260px): the panel gives up width so the page keeps about 700px */
.sb-customize.is-pushing .sb-customize-sheet {
  width: min(680px, calc(100vw - 736px));   /* 700px of page, a scroll bar and the 16px gap */
}

.sb-customize[data-state="open"] .sb-customize-sheet {
  transform: none;
}

.sb-customize .sb-customize-sheet.is-pulled {
  transition: none;
}

.sb-customize .sb-customize-grab {
  display: none;
}

.sb-customize .sb-customize-icon {
  flex: none;
  width: 22px;
  height: 22px;
}

/* ------------------------------------------------------------------ title and Close */
/* the title row and the button row stay put while the list scrolls between them (sticky: they are their own layer) */
.sb-customize .sb-customize-head {
  position: sticky;
  top: 0;
  z-index: 2;
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px 16px;
  padding: 24px 28px 16px;
  border-bottom: 1px solid var(--rule);
}

.sb-customize .sb-customize-titles {
  flex: 1 1 14rem;
  min-width: 0;
}

.sb-customize .sb-customize-title {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-head);
  font-weight: var(--w-head);
  font-size: var(--h2-size);
  line-height: 1.15;
  overflow-wrap: break-word;
}

.sb-customize .sb-customize-sub {
  margin: 8px 0 0;
  color: var(--soft);
  font-size: var(--text-quiet);
  line-height: 1.4;
}

.sb-customize .sb-customize-close {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 8px 16px 8px 12px;
  border: var(--btn2-bw, 1px) solid var(--btn2-line);
  border-radius: var(--btn-radius);
  background: var(--btn2-bg);
  box-shadow: var(--btn2-shadow);
  color: var(--btn2-ink);
  font: 600 1.0625rem/1.2 var(--font-ui);
  cursor: pointer;
}

.sb-customize .sb-customize-close .sb-customize-icon {
  color: var(--btn2-icon);
}

.sb-customize .sb-customize-close[aria-disabled="true"],
.sb-customize .sb-customize-reset[aria-disabled="true"] {
  cursor: progress;
  opacity: .72;
}

/* ------------------------------------------------------------------ the list (scrolls; title and buttons stay) */
.sb-customize .sb-customize-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 28px 20px;
}

.sb-customize .sb-customize-label {
  margin: 18px 0 4px;
  color: var(--ink);
  font-family: var(--font-head);
  font-weight: var(--w-head);
  font-size: 1.25rem;
  line-height: 1.2;
}

.sb-customize .sb-customize-hint {
  margin: 2px 0 6px;
  color: var(--soft);
  font-size: 1rem;
  line-height: 1.4;
}

.sb-customize .sb-customize-opens .sb-customize-hint {
  flex: 1 0 100%;
  margin: 0;
}

.sb-customize .sb-customize-opens,
.sb-customize .sb-customize-charts {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 12px 0 16px;
  border-bottom: 1px solid var(--rule);
}

.sb-customize .sb-customize-charts {
  border-top: 1px solid var(--rule);
  border-bottom: 0;
  margin-top: 4px;
}

.sb-customize .sb-customize-opens[hidden],
.sb-customize .sb-customize-charts[hidden],
.sb-customize .sb-customize-note[hidden],
.sb-customize .sb-customize-hint[hidden],
.sb-customize .sb-customize-msg[hidden],
.sb-customize .sb-customize-pending[hidden] {
  display: none;
}

.sb-customize .sb-customize-opens .sb-customize-label,
.sb-customize .sb-customize-charts .sb-customize-label {
  margin: 0;
}

.sb-customize .sb-customize-list {
  position: relative;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* one row: handle, name (and its Bars | Pie), Move up, Move down, the switch or the lock */
.sb-customize .sb-customize-row {
  --sbc-two-line: 0;
  position: relative;
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto 7.5rem;
  grid-template-areas: "grip main moves toggle";
  align-items: center;
  column-gap: 14px;
  row-gap: 8px;
  min-height: 64px;
  padding: 8px 0;
  border-bottom: 1px solid var(--rule);
  background: var(--pop-bg);
}

/* a new part of the page (for example the cards under both views): its rows start a little apart */
.sb-customize .sb-customize-row.is-part-start {
  margin-top: 18px;
  border-top: 1px solid var(--rule);
}

.sb-customize .sb-customize-grip {
  grid-area: grip;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 48px;
  color: var(--quiet);
  cursor: grab;
  touch-action: none;
}

.sb-customize .sb-customize-main {
  grid-area: main;
  min-width: 0;
}

/* the name: plain words (a press on it does nothing); the switch is named by it */
.sb-customize .sb-customize-name {
  display: flex;
  align-items: center;
  min-height: 48px;
  color: var(--ink);
  font: 600 var(--text-quiet)/1.3 var(--font-body);
  overflow-wrap: break-word;
}

/* a hidden card: its name calmer (still easy to read), so on and off are told apart without the switch too */
.sb-customize .sb-customize-row.is-off .sb-customize-name {
  color: var(--quiet);
  font-weight: 400;
}

.sb-customize .sb-customize-moves {
  grid-area: moves;
  display: flex;
  gap: 8px;
}

.sb-customize .sb-customize-move {
  min-width: 6.75rem;
  min-height: 48px;
  padding: 6px 12px;
  border: var(--btn2-bw, 1px) solid var(--btn2-line);
  border-radius: var(--btn-radius);
  background: var(--btn2-bg);
  box-shadow: var(--btn2-shadow);
  color: var(--btn2-ink);
  font: 500 1rem/1.2 var(--font-ui);
  white-space: nowrap;
  cursor: pointer;
}

.sb-customize .sb-customize-move:hover,
.sb-customize .sb-customize-close:hover,
.sb-customize .sb-customize-done:hover {
  filter: brightness(1.09);
}

/* the end of the list or of a part: still in the Tab order (aria-disabled), dashed, words still readable */
.sb-customize .sb-customize-move[aria-disabled="true"] {
  border-style: dashed;
  border-color: var(--line);
  background: transparent;
  box-shadow: none;
  color: var(--quiet);
  cursor: not-allowed;
  filter: none;
}

.sb-customize .sb-customize-toggle {
  grid-area: toggle;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 0;
}

/* the switch: a real checkbox with role="switch", drawn as a track and a knob; 48px tall to press */
.sb-customize .sb-customize-switch {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  width: 76px;
  height: 48px;
  margin: 0;
  border: 6px solid transparent;
  border-radius: 999px;
  background:
    radial-gradient(circle closest-side, var(--quiet) 88%, transparent 100%) 6px 50% / 26px 26px no-repeat padding-box,
    linear-gradient(var(--field-bg), var(--field-bg)) padding-box;
  box-shadow: inset 0 0 0 2px var(--quiet);
  cursor: pointer;
  transition: background-position 200ms ease-out;
}

.sb-customize .sb-customize-switch:checked {
  background:
    radial-gradient(circle closest-side, var(--btn1-ink) 88%, transparent 100%) calc(100% - 6px) 50% / 26px 26px no-repeat padding-box,
    var(--btn1-bg) padding-box;
  box-shadow: inset 0 0 0 2px var(--btn1-line);
}

.sb-customize .sb-customize-lock {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  color: var(--warn);               /* the gold of each look that still reads 4.5:1 on its own sheet (cream too) */
  font: 600 1rem/1.2 var(--font-ui);
  white-space: nowrap;
}

/* ------------------------------------------------------------------ the two-way choices (Bars | Pie, Simple | Breakdown) */
.sb-customize .sb-customize-seg {
  display: inline-flex;
  gap: 4px;
  margin-top: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: calc(var(--btn-radius) + 3px);
  background: var(--box-bg);
}

.sb-customize .sb-customize-opens .sb-customize-seg,
.sb-customize .sb-customize-charts .sb-customize-seg {
  margin-top: 0;
}

.sb-customize .sb-customize-seg [role="radio"] {
  min-width: 5.5rem;
  min-height: 48px;
  padding: 6px 16px;
  border: 1px solid transparent;
  border-radius: var(--btn-radius);
  background: transparent;
  color: var(--ink);
  font: 600 1rem/1.2 var(--font-ui);
  cursor: pointer;
}

.sb-customize .sb-customize-seg.is-wide [role="radio"] {
  min-width: 8.5rem;
}

/* the picked choice wears the same gold as the page's own Simple / Breakdown and Bars / Pie switch (joining,
   Oct 17: one "selected" look in the whole app; --p3-pill-gold-* live in portal.css) */
.sb-customize .sb-customize-seg [role="radio"][aria-checked="true"] {
  border-color: var(--p3-pill-gold-line);
  background: var(--p3-pill-gold-bg);
  box-shadow: none;
  color: var(--p3-pill-gold-ink);
}

/* ------------------------------------------------------------------ the note (client default) */
.sb-customize .sb-customize-note {
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;                 /* its own box, like each row, while the list scrolls under the foot */
  margin: 14px 0 0;
  color: var(--soft);
  font-size: 1rem;
  line-height: 1.4;
}

.sb-customize .sb-customize-note .sb-customize-icon {
  width: 24px;
  height: 24px;
  color: var(--accent);
}

/* ------------------------------------------------------------------ the foot: Reset to default, Done */
.sb-customize .sb-customize-foot {
  position: sticky;
  bottom: 0;
  z-index: 2;
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 16px;
  padding: 14px 28px 18px;
  border-top: 1px solid var(--rule);
  background: var(--pop-bg);
}

.sb-customize .sb-customize-msg,
.sb-customize .sb-customize-pending {
  margin: 0;
  font-size: 1rem;
  line-height: 1.4;
}

.sb-customize .sb-customize-msg {
  flex: 1 0 100%;
  color: var(--bad);
}

/* the owner's draft for a client that is not saved yet: on the line of "Reset to default", which it pushes right */
.sb-customize .sb-customize-pending {
  flex: 1 1 auto;
  color: var(--warn);
  font-weight: 600;
}

.sb-customize .sb-customize-reset {
  min-height: 48px;
  padding: 8px 6px;
  border: 1px solid transparent;
  border-radius: var(--btn-radius);
  background: none;
  color: var(--soft);
  font: 500 1.0625rem/1.2 var(--font-ui);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 5px;
  cursor: pointer;
}

.sb-customize .sb-customize-reset:hover {
  border-color: var(--line);
}

.sb-customize .sb-customize-done {
  min-width: 11rem;
  min-height: var(--btn-h, 56px);
  padding: 9px 28px;
  border: var(--btn-bw, 1px) solid var(--btn4-line);
  border-radius: var(--btn-radius);
  background: var(--btn4-bg);
  box-shadow: var(--btn4-shadow);
  color: var(--btn4-ink);
  font: var(--w-btn4) var(--text-btn)/1.2 var(--font-ui);
  cursor: pointer;
}

.sb-customize .sb-customize-done[aria-disabled="true"] {
  cursor: progress;
}

/* ------------------------------------------------------------------ words for screen readers only */
.sb-customize .sb-customize-live,
.sb-customize .sb-customize-sr,
.sb-customize-page-live {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ------------------------------------------------------------------ dragging a row */
.sb-customize .sb-customize-row.is-dragging {
  z-index: 2;
  border-radius: var(--btn-radius);
  box-shadow: var(--panel-shadow);
  cursor: grabbing;
}

.sb-customize .sb-customize-row.is-dragging .sb-customize-grip {
  cursor: grabbing;
  color: var(--accent);
}

/* a touch screen moves rows with Move up / Move down only: no handle that does nothing */
@media (pointer: coarse) {
  .sb-customize .sb-customize-grip {
    visibility: hidden;
  }
}

/* ------------------------------------------------------------------ a narrow panel: each row on two lines (as O2) */
/* in rem: a larger browser font size makes the panel "narrow" sooner, so the words get the room */
@container sbc (max-width: 40rem) {
  .sb-customize .sb-customize-row {
    --sbc-two-line: 1;               /* customize-pass3.js reads it: the switch comes before Move up in the Tab order */
    grid-template-columns: 24px minmax(0, 1fr) auto;
    grid-template-areas:
      "grip main toggle"
      "grip moves moves";
    margin: 0 0 12px;
    padding: 10px 14px 12px 8px;
    border: 1px solid var(--panel-line);
    border-radius: calc(var(--btn-radius) + 5px);
    background: var(--box-bg);
  }

  .sb-customize .sb-customize-row.is-part-start {
    margin-top: 22px;
    border-top: 1px solid var(--panel-line);
  }

  .sb-customize .sb-customize-row.is-dragging {
    background: var(--pop-bg);
  }

  .sb-customize .sb-customize-grip,
  .sb-customize .sb-customize-toggle {
    align-self: start;
  }

  .sb-customize .sb-customize-list {
    margin-top: 8px;
  }

  .sb-customize .sb-customize-moves {
    flex-wrap: wrap;
  }

  .sb-customize .sb-customize-move {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 12rem;
  }

  /* the choices fill the width; when the words do not fit side by side, each takes its own line */
  .sb-customize .sb-customize-seg {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
  }

  .sb-customize .sb-customize-seg [role="radio"],
  .sb-customize .sb-customize-seg.is-wide [role="radio"] {
    flex: 1 1 auto;
    min-width: 0;
  }
}

/* ------------------------------------------------------------------ phone and tablet: the bottom sheet (as O2) */
@media (max-width: 899px) {
  .sb-customize[data-state="open"] .sb-customize-scrim {
    opacity: 1;
  }

  .sb-customize .sb-customize-sheet {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: min(100%, 760px);
    max-height: 88vh;
    max-height: 88dvh;
    margin: 0 auto;
    border-bottom: 0;
    border-radius: var(--panel-radius) var(--panel-radius) 0 0;
    transform: translateY(calc(100% + 24px));
  }

  .sb-customize .sb-customize-grab {
    display: flex;
    flex: none;
    justify-content: center;
    padding: 10px 0 0;
    touch-action: none;
  }

  .sb-customize .sb-customize-grab span {
    width: 56px;
    height: 5px;
    border-radius: 3px;
    background: var(--line);
  }

  .sb-customize .sb-customize-head {
    padding: 12px 20px 14px;
    touch-action: none;
  }

  .sb-customize .sb-customize-body {
    padding: 4px 16px 16px;
  }

  .sb-customize .sb-customize-foot {
    padding: 12px 20px calc(14px + env(safe-area-inset-bottom, 0px));
  }

  /* the gold button is as wide as its words on one line: when "Save for <name>" does not fit beside the reset it
     takes its own full row under it (and only a very long name breaks onto two lines) */
  .sb-customize .sb-customize-done {
    flex: 1 1 auto;
  }
}

/* ------------------------------------------------------------------ phone sizes: no text under 18px */
@media (max-width: 699px) {
  .sb-customize .sb-customize-title {
    font-size: 1.625rem;
  }

  .sb-customize .sb-customize-close,
  .sb-customize .sb-customize-move,
  .sb-customize .sb-customize-lock,
  .sb-customize .sb-customize-seg [role="radio"],
  .sb-customize .sb-customize-note,
  .sb-customize .sb-customize-hint,
  .sb-customize .sb-customize-msg,
  .sb-customize .sb-customize-pending,
  .sb-customize .sb-customize-reset {
    font-size: 1.125rem;
  }

  .sb-customize .sb-customize-close {
    padding: 8px 14px 8px 10px;
  }
}

/* ------------------------------------------------------------------ a short screen (phone sideways, big zoom) */
/* the whole sheet scrolls, so the list gets the height instead of one or two rows; the title row stays on top
   (compact, so "Close" is always in sight) and the buttons come at the end of the list.
   In em: 520px at the usual text size, and taller when the browser's own text size is set larger */
@media (max-height: 32.5em) {
  .sb-customize .sb-customize-sheet {
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .sb-customize .sb-customize-head {
    align-items: center;
    padding-top: 10px;
    padding-bottom: 10px;
    background: var(--pop-bg);
  }

  .sb-customize .sb-customize-sub {
    display: none;                  /* still the dialog's description for screen readers */
  }

  .sb-customize .sb-customize-foot {
    position: static;
  }

  .sb-customize .sb-customize-body {
    flex: none;
    min-height: auto;
    overflow: visible;
  }

  .sb-customize .sb-customize-grab {
    padding-top: 6px;
  }

  .sb-customize .sb-customize-foot {
    padding-top: 10px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }
}

/* ------------------------------------------------------------------ less movement */
@media (prefers-reduced-motion: reduce) {
  .sb-customize .sb-customize-scrim,
  .sb-customize .sb-customize-sheet,
  .sb-customize .sb-customize-switch {
    transition: none;
  }
}

/* ------------------------------------------------------------------ Windows high contrast (forced colors) */
/* the system drops backgrounds and shadows there, so on and off would look the same: the switch and the picked
   choice draw themselves with the system colors */
@media (forced-colors: active) {
  .sb-customize .sb-customize-switch {
    forced-color-adjust: none;
    background:
      radial-gradient(circle closest-side, GrayText 88%, transparent 100%) 6px 50% / 26px 26px no-repeat padding-box,
      linear-gradient(Canvas, Canvas) padding-box;
    box-shadow: inset 0 0 0 2px ButtonText;
  }

  .sb-customize .sb-customize-switch:checked {
    background:
      radial-gradient(circle closest-side, HighlightText 88%, transparent 100%) calc(100% - 6px) 50% / 26px 26px no-repeat padding-box,
      linear-gradient(Highlight, Highlight) padding-box;
    box-shadow: inset 0 0 0 2px Highlight;
  }

  .sb-customize .sb-customize-switch:focus-visible,
  .sb-customize .sb-customize-seg [role="radio"]:focus-visible {
    outline-color: CanvasText;
  }

  .sb-customize .sb-customize-seg [role="radio"][aria-checked="true"] {
    forced-color-adjust: none;
    border-color: Highlight;
    background: Highlight;
    box-shadow: none;
    color: HighlightText;
  }

  .sb-customize .sb-customize-grab span {
    forced-color-adjust: none;
    background: CanvasText;
  }

  .sb-customize .sb-customize-row.is-off .sb-customize-name {
    color: GrayText;
  }
}
