/* css/lib/flow-knob.css · the red ball and its flow curve (component .flow, used by 06-flow and 26-flow-control) */
@layer components {
  .flow {
    display: grid;
    row-gap: var(--space-2xl);
    align-items: center;
  }
  @media (min-width: 64em) {
    .flow { grid-template-columns: repeat(12, minmax(0, 1fr)); column-gap: var(--gap-columns); }
    .flow__control { grid-column: 1 / span 4; }
    .flow__chart { grid-column: 6 / -1; }
  }
  /* desktop, one instrument: five shared rows (note | dial + plot | readout + caption | hint | reset),
     so the dial's centre and the plot's centre sit on one line */
  @supports (grid-template-rows: subgrid) {
    @media (min-width: 64em) {
      .flow { grid-template-rows: repeat(5, auto); row-gap: 0; align-items: start; }
      .flow__control, .flow__chart { grid-row: 1 / span 5; grid-template-rows: subgrid; row-gap: 0; }
      .flow__chart { align-items: start; }
      .flow__note { grid-row: 1; }
      .flow__plot { grid-row: 2; align-self: center; }
      .flow .flow__chart .caption { grid-row: 3; }
      .flow__dial { grid-row: 2; align-self: center; }
      .flow .flow__readout, .flow__control noscript { grid-row: 3; }
      .flow .flow__hint { grid-row: 4; }
      .flow__reset { grid-row: 5; }
    }
  }

  /* control: dial, readout, hint, reset, stacked and centred like an instrument */
  .flow__control {
    display: grid;
    justify-items: center;
    text-align: center;
    min-width: 0;
  }
  .flow__dial {
    position: relative;
    inline-size: clamp(180px, 56vw, 240px);
    aspect-ratio: 1;
    border-radius: 50%;
    touch-action: pan-y pinch-zoom;   /* a swipe over the dial still scrolls the page; only the grip on the ball holds the finger */
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
  }
  @media (min-width: 64em) { .flow__dial { inline-size: clamp(260px, 24vw, 320px); } }
  /* the grip: an invisible hit area that rides on the red ball (made and moved by flowKnob) */
  .flow__grip {
    position: absolute;
    inline-size: max(48px, 30%);
    aspect-ratio: 1;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    touch-action: none;
  }
  .flow__dial svg { inline-size: 100%; block-size: auto; overflow: visible; }
  .flow__dial:focus { outline: none; }
  .flow__dial:focus-visible { outline: var(--focus-w) solid var(--color-focus); outline-offset: 6px; }
  .flow__dial.is-dragging { cursor: grabbing; }
  .flow:not(.is-ready) .flow__dial { cursor: default; touch-action: auto; }

  .flow__plate { fill: var(--color-paper-2); stroke: var(--color-rule); stroke-width: 1; }
  .flow__fill { fill: none; stroke: var(--color-accent); stroke-width: 2; stroke-linecap: butt; stroke-dasharray: 1; stroke-dashoffset: 0; }
  .flow__halo {
    fill: none; stroke: transparent; stroke-width: 1;
    transform-box: fill-box; transform-origin: center; transform: scale(.82);
    transition: stroke var(--dur-short) var(--ease-out), transform var(--dur-short) var(--ease-out);
  }
  /* the detent the lever rests on lights up (data-flow-pos comes from flowKnob) */
  .flow__tick { fill: none; stroke: transparent; stroke-width: 2.5; stroke-linecap: round; transition: stroke var(--dur-short) var(--ease-out); }
  .flow[data-flow-pos="0"] .flow__tick--0,
  .flow[data-flow-pos="1"] .flow__tick--1,
  .flow[data-flow-pos="2"] .flow__tick--2,
  .flow[data-flow-pos="3"] .flow__tick--3 { stroke: var(--color-ink); }
  @media (hover: hover) { .flow.is-ready .flow__dial:hover .flow__halo { stroke: color-mix(in oklch, var(--color-ink-2) 45%, transparent); transform: none; } }
  .flow__dial:focus-visible .flow__halo,
  .flow__dial.is-pressed .flow__halo { stroke: color-mix(in oklch, var(--color-ink-2) 70%, transparent); transform: none; }

  .flow .flow__readout {
    margin-block-start: var(--space-lg);
    min-block-size: calc(2lh);
    max-inline-size: 26ch;
    font-size: var(--text-lead);
    line-height: var(--leading-lead);
    color: var(--color-ink-2);
    text-wrap: balance;
  }
  .flow .flow__hint { margin-block-start: var(--space-xs); max-inline-size: 34ch; font: 500 var(--text-sm)/1.4 var(--font-body); color: var(--color-muted); text-wrap: balance; }
  .flow__reset { margin-block-start: var(--space-lg); }
  .flow__reset[aria-disabled="true"] { opacity: .5; cursor: default; }
  .flow .flow__control noscript .caption { margin-block-start: var(--space-lg); font: 500 var(--text-sm)/1.4 var(--font-body); color: var(--color-muted); }

  /* chart */
  .flow__chart { min-width: 0; display: grid; gap: var(--space-md); }
  /* the note leads the chart, on the y axis line (the plot starts at x 48 of 600 = 8%) */
  .flow .flow__note {
    padding-inline-start: 8%;
    margin-block-end: var(--space-lg);
    font: 300 var(--text-lead)/var(--leading-lead) var(--font-display);
    color: var(--color-ink);
    max-width: none;
    text-wrap: balance;
  }
  .flow__plot { position: relative; }
  .flow__plot svg { inline-size: 100%; block-size: auto; overflow: visible; }
  .flow__axis { fill: none; stroke: color-mix(in oklch, var(--color-line) 50%, transparent); stroke-width: 1; vector-effect: non-scaling-stroke; }
  .flow__ref { fill: none; stroke: var(--color-edge); stroke-width: 1; stroke-dasharray: 3 5; vector-effect: non-scaling-stroke; }
  .flow__line { fill: none; stroke: var(--color-ink); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
  .flow__area { fill: url(#flow-area-grad); stroke: none; }   /* crema light falling off under the curve; gradient lives in the section's SVG */
  .flow__area-stop { stop-color: var(--color-accent); stop-opacity: .14; }
  .flow__area-stop--end { stop-opacity: 0; }
  .flow__dot { fill: var(--color-accent); }
  .flow__static .flow__line { stroke-width: 1.5; }
  .flow__static .flow__line--0 { stroke: var(--color-muted); }
  .flow__static .flow__line--2 { stroke: var(--color-ink-2); }

  /* labels are HTML over the plot so they keep their size on a phone */
  .flow__lbls { position: absolute; inset: 0; pointer-events: none; }
  .flow__lbl {
    position: absolute;
    font: 500 var(--text-sm)/1.2 var(--font-body);
    white-space: nowrap;
    color: var(--color-ink-2);
  }
  .flow__lbl--axis { color: var(--color-muted); }
  @media (max-width: 39.99em) { .flow__lbl { font-size: var(--text-xs); } }
  .flow__lbl--flow  { left: 8%;  top: 8%;    transform: translate(-50%, calc(-100% - var(--space-xs))); }   /* above the y axis top */
  .flow__lbl--time  { left: 96%; top: 89.5%; transform: translateX(-100%); }   /* under the x axis end */
  .flow__lbl--full  { left: 30%; top: 20%;   transform: translateY(calc(-100% - var(--space-xs))); }        /* above the plateau */
  .flow__lbl--gentle{ left: 96%; top: 62%;   transform: translate(-100%, var(--space-xs)); }                 /* under the gentle curve's flat end */
  .flow__lbl--s0    { left: 96%; top: 64.5%; transform: translateX(-100%); color: var(--color-muted); }
  .flow__lbl--s2    { left: 96%; top: 37.5%; transform: translate(-100%, -100%); }
  .flow__lbl--s3    { left: 96%; top: 20%;   transform: translate(-100%, -100%); color: var(--color-ink); }

  /* the plot starts at x 48 of 600 and ends at 576: note and caption align to the axes */
  .flow .flow__chart .caption { margin-block-start: var(--space-lg); padding-inline-start: 8%; text-align: start; font: 500 var(--text-sm)/1.4 var(--font-body); color: var(--color-muted); max-width: none; }

  /* phones: the curve sits right under the dial, so the thumb and the reshaping curve share one screen
     (DOM order stays dial, readout, hint, reset, chart; only the chart is lifted, and it holds no focusable element) */
  @media (max-width: 63.99em) {
    .flow { row-gap: 0; }
    .flow__control { display: contents; }
    .flow__dial { order: 1; justify-self: center; }
    .flow .flow__readout { order: 2; justify-self: center; margin-block-start: var(--space-md); }
    .flow__control noscript { order: 2; justify-self: center; }
    .flow__chart { order: 3; justify-self: stretch; margin-block-start: var(--space-xl); }
    .flow .flow__hint { order: 4; justify-self: center; margin-block-start: var(--space-xl); }
    .flow__reset { order: 5; justify-self: center; }
    .flow .flow__chart .caption { margin-block-start: var(--space-sm); }
    .flow .flow__note { margin-block-end: var(--space-md); }
  }

  /* one switch for both states: without a working flowKnob there is no fake control */
  .flow:not(.is-ready) :is(.flow__live, .flow__readout, .flow__hint, .flow__reset) { display: none; }
  .flow.is-ready .flow__static { display: none; }

  @media (forced-colors: active) {
    .flow__fill, .flow__line { stroke: CanvasText; }
    .flow__dot { fill: CanvasText; }
    .flow__area { fill: transparent; }
  }
}
