/* ==========================================================================
   CardiSim: how the instrument feels under the hand (feel.js, home-feel.js).

   Nothing here changes what a page says or where a thing sits; it is the
   motion and the small answers a press gets. Every rule is CardiSim's own:
   the shared study modules and MediSim never load this sheet.

   The house rules for motion, kept here as everywhere else on the site:
   things print, feed, seat and light; nothing floats, lifts or bounces
   for its own sake. Transforms, opacity and clip-path only, so Firefox
   and a phone stay cool. With prefers-reduced-motion none of it runs and
   every page is simply already there.
   ========================================================================== */

/* ── Small things, everywhere ─────────────────────────────────────────── */
.cardisim ::selection { background: color-mix(in oklab, var(--red) 46%, var(--cap)); color: var(--ink); }
.cardisim :is(.csSecTitle, .csTrackName, .csConsoleName, .tourTitle) { text-wrap: balance; }
.cardisim .csSec { scroll-margin-top: 16px; }

/* A key that leads somewhere leans its arrow that way under the pointer. */
.cardisim .key .nf-go { transition: translate var(--m-release, 150ms) var(--e-settle, ease-out); }
@media (hover: hover) and (pointer: fine) {
  .cardisim .key:hover:not(:disabled):not([aria-disabled="true"]) .nf-go { translate: 3px 0; }
}
.cardisim .key:active .nf-go { translate: 5px 0; transition-duration: var(--m-press, 24ms); }

@media (prefers-reduced-motion: no-preference) {
  /* One page hands over to the next with a short cross-fade instead of a
     blank flash. Browsers without cross-page transitions just load the page. */
  @view-transition { navigation: auto; }
  ::view-transition-old(root), ::view-transition-new(root) { animation-duration: 170ms; animation-timing-function: linear; }

  /* A study mode opens over the home the same way: it comes up out of the
     page instead of cutting to it. Opacity only, so the mode's own sticky
     bar and the 3D heart are never moved. Changing mode inside the bar
     keeps the same animation name, so it does not run again. */
  .cardisim #monitorOverlay:is(.quiz-mode, .study-mode, .trainers-mode),
  .cardisim :is(#course, #heartLab):not([hidden]) { animation: csModeIn 170ms linear; }
}

/* ── The rhythm board: a name on the key under the hand ───────────────── */
.csBoardGrid { position: relative; }
.csCellTip {
  position: absolute; z-index: 2; left: 0; top: 0; pointer-events: none;
  translate: calc(var(--x, 0px) - 50%) calc(var(--y, 0px) - 100% - 9px);
  display: grid; gap: 3px; justify-items: center; white-space: nowrap;
  padding: 6px 10px 7px; border-radius: var(--r);
  background: var(--ink); color: var(--cap);
  opacity: 0; transition: opacity var(--m-leave, 110ms) linear;
}
.csCellTip.is-on { opacity: 1; transition-duration: var(--m-hover, 90ms); }
.csCellTip::after {
  content: ""; position: absolute; left: calc(50% + var(--nib, 0px)); top: 100%; width: 9px; height: 9px;
  translate: -50% -5px; rotate: 45deg; background: var(--ink); border-radius: 0 0 2px 0;
}
.csCellTipName { font: 650 var(--fs-small)/1.2 var(--font-ui); }
.csCellTipTally {
  font: 600 var(--fs-legend)/1.2 var(--font-legend, var(--font-mono)); letter-spacing: var(--tr-legend, .06em);
  text-transform: uppercase; font-variant-numeric: tabular-nums; color: color-mix(in oklab, var(--cap) 66%, var(--ink));
}
.csCellTip[data-state="scored"] .csCellTipTally { color: var(--blue); }
.csCellTip[data-miss] .csCellTipTally { color: var(--red); }
@media (pointer: coarse) { .csCellTip { display: none; } }

/* The chosen key carries a lamp bar, and the bar blinks on every QRS of the
   rhythm playing above it (home-feel.js beat()). */
.csCell { position: relative; }
.csCell[aria-pressed="true"]::after {
  content: ""; position: absolute; left: 1px; right: 1px; bottom: -2px; height: 2px;
  background: var(--led-red); opacity: 0; border-radius: 0 0 var(--r) var(--r);
}
.csCell.is-beat[aria-pressed="true"]::after { animation: csBeatBar 300ms linear; }
@keyframes csBeatBar { 0%, 28% { opacity: 1; } 100% { opacity: 0; } }

/* An answered rhythm's key, back from a round: its well washes amber once. */
.csCell.is-fresh .csMeter { animation: csFresh 1100ms var(--e-settle, ease-out); }
@keyframes csFresh {
  0%, 30% { box-shadow: var(--well-shadow), 0 0 0 2px var(--led-amber); }
  100% { box-shadow: var(--well-shadow), 0 0 0 2px transparent; }
}

@media (prefers-reduced-motion: no-preference) {
  /* Power on: once a visit the board runs a lamp test, key by key, left to
     right, and each meter then rises to where the learner left it. */
  .csBoardGrid.is-booting .csDots i { animation: csLamp 380ms linear both; animation-delay: calc(var(--i, 0) * 26ms + var(--d, 0) * 36ms); }
  .csBoardGrid.is-booting .csDots i:nth-child(2) { --d: 1; }
  .csBoardGrid.is-booting .csDots i:nth-child(3) { --d: 2; }
  .csBoardGrid.is-booting .csCell[data-state="scored"] .csDots { display: flex; animation: csLampOut 1ms linear both; animation-delay: calc(var(--i, 0) * 26ms + 460ms); }
  .csBoardGrid.is-booting .csMeterFill {
    transform-origin: 50% 100%;
    animation: csRise 520ms var(--e-seat, ease-out) both; animation-delay: calc(var(--i, 0) * 26ms + 440ms);
  }
  .csBoardGrid.is-booting :is(.csCellAbbr, .csCellPct) { animation: csInk 240ms linear both; animation-delay: calc(var(--i, 0) * 26ms + 120ms); }

  /* A new strip feeds in from the recorder's side. */
  .csConsoleStrip > .is-feeding { animation: csFeed 300ms var(--e-seat, ease-out); }
  .csConsoleName.is-named { animation: csNamed var(--m-seat, 180ms) var(--e-seat, ease-out); }

  /* The landing's course map prints as it comes into view: each thumbnail
     strip runs out of the recorder left to right. */
  .has-reveal .csTrack:not(.is-in) .csThumb { clip-path: inset(0 100% 0 0); }
  .has-reveal .csTrack .csThumb { transition: clip-path 820ms steps(41, end) 80ms; clip-path: inset(0 0 0 0); }
  .has-reveal .csTrack:not(.is-in) .csTrackNo { border-left-color: var(--rule); }
  .has-reveal .csTrack .csTrackNo { transition: border-left-color 300ms linear 80ms; }
  .has-reveal .csTrack:not(.is-in) .csChips li { opacity: 0; translate: 0 4px; }
  .has-reveal .csTrack .csChips li { transition: opacity 200ms linear, translate 260ms var(--e-seat, ease-out); transition-delay: calc(520ms + var(--i, 0) * 45ms); }

  /* The home's trainer tiles: the picture answers the pointer. */
  @media (hover: hover) and (pointer: fine) {
    .trainerTile:hover .csArtStep { animation: csStep 460ms var(--e-spring, ease-out) both; animation-delay: calc(var(--i, 0) * 70ms); }
    .trainerTile:hover .csArtNeedle { animation: csNeedle 900ms var(--e-settle, ease-out) both; }
    .trainerTile:hover .csArtAnts { animation: csAnts 700ms linear infinite; }
  }
}
.csArtNeedle { transform-box: view-box; transform-origin: 220px 200px; }
@keyframes csModeIn { from { opacity: 0; } }
@keyframes csLamp { 0% { background: transparent; box-shadow: var(--lens-off); } 12%, 62% { background: var(--led-amber); box-shadow: var(--lens); } }
@keyframes csLampOut { from { opacity: 1; } to { opacity: 0; } }
@keyframes csRise { from { transform: scaleY(0); } }
@keyframes csInk { from { opacity: .25; } }
@keyframes csFeed { from { transform: translateX(34px); opacity: .2; } }
@keyframes csStep { from { transform: translateY(14px); opacity: .35; } }
@keyframes csNeedle { 0% { transform: rotate(-118deg); } 55% { transform: rotate(9deg); } 78% { transform: rotate(-3deg); } 100% { transform: none; } }
@keyframes csAnts { to { stroke-dashoffset: -18; } }

/* ── The readouts: a figure that changed says by how much ─────────────── */
.csReadouts dd { position: relative; }
.csDelta {
  position: absolute; left: 50%; bottom: 100%; translate: -50% 0; pointer-events: none; white-space: nowrap;
  padding: 2px 6px 3px; border-radius: var(--r-sm); background: var(--green-ink); color: var(--cap);
  font: 700 var(--fs-legend)/1 var(--font-legend, var(--font-mono)); letter-spacing: .04em;
  animation: csDelta 2200ms var(--e-settle, ease-out) both;
}
@keyframes csDelta { 0% { opacity: 0; translate: -50% 6px; } 10%, 78% { opacity: 1; translate: -50% -3px; } 100% { opacity: 0; translate: -50% -9px; } }
@media (prefers-reduced-motion: reduce) { .csDelta { animation-name: csDeltaStill; } }
@keyframes csDeltaStill { 0%, 85% { opacity: 1; } 100% { opacity: 0; } }

/* ── A phone's home: the three sections on a strip of keys at the thumb ── */
.csDock { display: none; }
@media (max-width: 760px) {
  .csDock {
    position: fixed; z-index: 2; left: 0; right: 0; bottom: 0;
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 3px;
    padding: 6px max(10px, env(safe-area-inset-left)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-right));
    background: var(--chassis); border-top: 1px solid var(--rule-2);
    box-shadow: 0 -1px 0 var(--cap) inset;
    transition: translate 220ms var(--e-seat, ease-out);
  }
  .csDock[data-away] { translate: 0 110%; transition-timing-function: var(--e-leave, ease-in); }
  .csDockKey {
    display: flex; align-items: center; justify-content: center; gap: 7px; min-height: 42px; min-width: 0;
    border: 1px solid transparent; border-radius: var(--r); background: transparent; color: var(--ink-2);
    font: 600 var(--fs-small)/1 var(--font-ui); text-decoration: none; -webkit-tap-highlight-color: transparent;
    transition: var(--key-transition);
  }
  .csDockKey .csDockNo { font: 600 10px/1 var(--font-code, monospace); color: var(--ink-3); }
  .csDockKey[aria-current="true"] { background: var(--cap); color: var(--ink); border-color: var(--rule-2); box-shadow: var(--key-shadow); }
  .csDockKey[aria-current="true"] .csDockNo { color: var(--red-ink); }
  .csDockKey:active { transform: translateY(1px); }
  .csDockKey:focus-visible { outline: 2px solid transparent; box-shadow: var(--focus); }
  .csHomePage .csPage { padding-bottom: calc(40px + 58px + env(safe-area-inset-bottom)); }
  /* A study mode is its own page: the strip leaves with the home under it. */
  body:has(#monitorOverlay:is(.quiz-mode, .study-mode, .trainers-mode), #heartLab:not([hidden]), #course:not([hidden]), dialog[open]) .csDock { display: none; }
}
@media (prefers-reduced-motion: reduce) { .csDock { transition: none; } }
@media print { .csDock, .csCellTip { display: none; } }

/* ==========================================================================
   The study screens, the forms and the tour (study-feel.js).

   The study modules are shared with MediSim and are not touched: these
   rules reach them from CardiSim's side only, under `.cardisim`. A step
   that changes feeds in from the side it came from (Next from the right,
   Back from the left, `--dx`), the way the home's strips do.
   ========================================================================== */

/* More below: a scrolling text panel fades its last lines while there is
   more to read, in place of a hard cut (study-feel.js sets data-more). */
.cardisim [data-more="true"] {
  -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 44px), transparent);
  mask-image: linear-gradient(to bottom, #000 calc(100% - 44px), transparent);
}

/* Progress cells and lamps change with a short settle, never a jump. */
.cardisim :is(.trainersTape i, .hlSteps i, .hlTape i, .cmPageLamps li, .cmPageLamps i) { transition: background-color 200ms linear, box-shadow 200ms linear; }

/* Forms: a field under the caret says so with its edge, and a message that
   arrives seats itself instead of appearing. */
.cardisim .input { transition: border-color var(--m-hover, 90ms) linear, box-shadow var(--m-release, 150ms) var(--e-settle, ease-out), background-color var(--m-hover, 90ms) linear; }
.cardisim .csField:focus-within > .csLabel { color: var(--ink); }
.cardisim .csLabel { transition: color var(--m-hover, 90ms) linear; }
.cardisim .switch, .cardisim .switch::after, .cardisim .switch::before { transition-duration: var(--m-release, 150ms); }

@media (prefers-reduced-motion: no-preference) {
  /* Trainers and the Heart Lab: the step's own parts feed in, a beat apart.
     The Back and Next keys never move, so a thumb can keep stepping. */
  .cardisim #trainersScroll.is-step > *,
  .cardisim .hlLesson.is-step > :is(.hlStepTitle, .hlBody, .hlTask, .hlCheck) {
    animation: csStepFeed 240ms var(--e-seat, ease-out) backwards;
  }
  .cardisim :is(#trainersScroll, .hlLesson).is-step > :nth-child(2) { animation-delay: 30ms; }
  .cardisim :is(#trainersScroll, .hlLesson).is-step > :nth-child(3) { animation-delay: 70ms; }
  .cardisim :is(#trainersScroll, .hlLesson).is-step > :nth-child(4) { animation-delay: 110ms; }
  .cardisim :is(#trainersScroll, .hlLesson).is-step > :nth-child(n+5) { animation-delay: 140ms; }
  /* A trainer's diagram draws its own strokes in as it arrives. */
  .cardisim #trainersScroll.is-step #trainersDiagram svg { animation: csDraw 420ms var(--e-settle, ease-out) 60ms backwards; }

  /* The course: every page is a fresh sheet, so its strip and its bench
     feed in; the heading stays where the eye left it. */
  .cardisim .cmLesson > .cmStage > * { animation: csStepFeed 260ms var(--e-seat, ease-out) backwards; }
  .cardisim .cmLesson > .cmStage > :nth-child(2) { animation-delay: 70ms; }
  .cardisim .cmLesson > .cmStage > :nth-child(n+3) { animation-delay: 120ms; }
  .cardisim .cmLesson .cmText > p { animation: csInkIn 260ms linear backwards; animation-delay: calc(120ms + var(--p, 0) * 70ms); }
  .cardisim .cmLesson .cmText > p:nth-child(2) { --p: 1; }
  .cardisim .cmLesson .cmText > p:nth-child(3) { --p: 2; }
  .cardisim .cmLesson .cmText > p:nth-child(n+4) { --p: 3; }
  /* Next is ready: its edge lights once, so a learner who was reading sees it. */
  .cardisim .cmNext.is-ready { animation: csReady 700ms var(--e-settle, ease-out); }

  /* The quiz's run strip and the Field Guide already move (their own
     sheets). CardiSim adds the monitor's answer: on a right name the
     screen's edge lights green once, on a miss it stays dark. */
  .cardisim #monitorOverlay.quiz-mode.is-right .monitorCard { outline: 2px solid transparent; outline-offset: 2px; animation: csScreenRight 620ms linear; }

  /* Sign-in, sign-up, reset: the card arrives, and whatever it answers
     with (an error, a sent note, the saved tick) seats itself. */
  .cardisim .csFace:not([hidden]) > :is(.csAuthTitle, .csAuthSub) { animation: csInkIn 240ms linear backwards; }
  .cardisim .csFace:not([hidden]) > .csAuthSub { animation-delay: 60ms; }
  .cardisim :is(.csFormError, .csDone, .csNotice):not([hidden]):not(:empty) { animation: csSeatIn 200ms var(--e-seat, ease-out) backwards; }
  .cardisim .csSent:not([hidden]) > * { animation: csStepFeed 260ms var(--e-seat, ease-out) backwards; }
  .cardisim .csSent:not([hidden]) > :nth-child(2) { animation-delay: 60ms; }
  .cardisim .csSent:not([hidden]) > :nth-child(n+3) { animation-delay: 120ms; }

  /* The account page prints its panels one after another. */
  .cardisim .csAcctRow { animation: csInkIn 260ms linear backwards; animation-delay: calc(var(--row, 0) * 55ms); }
  .cardisim .csAcctRow:nth-of-type(2) { --row: 1; }
  .cardisim .csAcctRow:nth-of-type(3) { --row: 2; }
  .cardisim .csAcctRow:nth-of-type(4) { --row: 3; }
  .cardisim .csAcctRow:nth-of-type(n+5) { --row: 4; }

  /* The tour: until it is pressed, the Play key's lamp breathes; the
     scene keys light in step with the film. */
  .cardisim .tourPlay.key:not([hidden])::before {
    content: ""; flex: none; width: 8px; height: 8px; border-radius: 50%; margin-right: 2px;
    background: var(--cap); box-shadow: inset 0 0 0 1px rgba(0,0,0,.25);
    animation: csBreathe 1.6s ease-in-out infinite;
  }
  .cardisim .tourStop { transition: var(--key-transition), background-color 200ms linear; }
  .cardisim .tourStop[data-state="on"] .led, .cardisim .tourStop[data-state="on"]::before { animation: csLampOn 260ms linear; }
}
@keyframes csStepFeed { from { transform: translateX(var(--dx, 18px)); opacity: 0; } }
@keyframes csInkIn { from { opacity: 0; } }
@keyframes csSeatIn { from { opacity: 0; transform: translateY(-5px); } }
@keyframes csDraw { from { opacity: .15; transform: scale(.985); } }
@keyframes csReady { 0%, 35% { box-shadow: var(--key-shadow), 0 0 0 3px color-mix(in oklab, var(--blue) 60%, transparent); } 100% { box-shadow: var(--key-shadow), 0 0 0 3px transparent; } }
@keyframes csScreenRight { 0%, 30% { outline-color: var(--led-green); } 100% { outline-color: transparent; } }
@keyframes csBreathe { 0%, 100% { opacity: .35; } 50% { opacity: 1; } }
@keyframes csLampOn { 0% { opacity: .2; } 40% { opacity: 1; } 70% { opacity: .5; } 100% { opacity: 1; } }
