/* app/css/motion.css — Apple-Settings-style tactile motion on top of the neo-brutalist look.
   Loaded after screens-viewer.css (and before the desktop stylesheet), so it wins ties; every interaction rule
   also carries an `html` prefix (+1 type specificity) so it beats the older per-component :hover / :active
   rules in brutal.css, components.css, layout.css and screens-*.css WITHOUT editing them.
   Spec: hover lifts (pointer devices only), press scales (all inputs), springs back with a little overshoot.
   Only transform, opacity and box-shadow are ever animated (colour only where an older rule already did).
   Brutalist rules are kept: hard offset shadows only, never blurry ones, on cards and buttons.

   Sections: 1 tokens · 2 touch polish · 3 transitions · 4 hover · 5 press · 6 tab bar, top bar, segmented
             · 7 frosted glass · 8 concentric radii · 9 view transitions · 10 reduced motion */

/* ── 1. Tokens ───────────────────────────────────────────────────────────────────────────────────────── */
:root {
  --spring-snappy: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-apple: cubic-bezier(0.25, 1, 0.5, 1);
  --press-dur: 120ms;
  --hover-dur: 260ms;
  --press-scale: 0.97;
  --press-scale-sm: 0.9; /* icon-sized targets (top bar buttons, tab icons) need a deeper dip to be felt */

  /* View transitions (section 9) */
  --vt-root-dur: 220ms;
  --vt-hero-dur: 320ms;
  /* ease-apple, not --spring-snappy: the hero group animates width/height/transform, and the spring's
     overshoot would make the morphing card briefly bigger than the header it lands in. */
  --vt-hero-ease: var(--ease-apple);

  /* Concentric radii (section 8): inner radius = outer radius - padding, never below --r-min */
  --r-outer: var(--radius, 7px);
  --r-min: 3px;
  --pad: 0px;

  /* Glass (section 7). Panels use paper at 82 %; the sheet scrim stays dark in BOTH themes, because a paper
     veil behind a paper bottom sheet would wash out the page and lose the sheet's edge. */
  --glass-mix: 82%;
  --scrim: color-mix(in srgb, #000 48%, transparent);
}

html[data-theme="dark"] {
  --scrim: color-mix(in srgb, #000 62%, transparent);
}

/* ── 2. Touch polish: every interactive element ──────────────────────────────────────────────────────── */
:where(
    a[href],
    button,
    [role="button"],
    [role="option"],
    [role="tab"],
    summary,
    select,
    input[type="checkbox"],
    input[type="radio"],
    .is-link,
    .b-check,
    .te-unit-box,
    .te-subject-label,
    .te-unit-label,
    .example-card.is-collapsed
  ) {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* The top-level pills are links/buttons that were only 38px tall; keep every tappable at least 48px. */
a.b-pill,
button.b-pill {
  min-height: 48px;
}

/* ── 3. Transitions (base state, so releasing a press springs back on touch screens too) ─────────────── */
/* Surfaces, grouped. Each group is repeated in sections 3-5 and 10 (CSS has no selector variables):
     CARD  cards that navigate        CTA   dark call-to-action bars       BTN  outlined buttons
     ROW   flat rows, pills, links    (.tp-topic.is-soon and plain .ch-card are not tappable: left out)       */
html :is(
    .b-card.is-link, .hm-subject, .ch-card.is-link, .tp-topic.is-link, .cal-item--exam,
    .subject-card, .menu-tile, .tool-tile,
    .b-cta, .tp-continue, .hm-next,
    .b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go, .proof-toggle-btn, .icon-btn, .btn-google,
    .search-result-option, .sem-pick__list li, .sem-pick__btn, a.b-pill, button.b-pill, a.b-chip, button.b-chip,
    .unit-pill, .btn-link, a.b-crumb, .b-topbar__home, .b-topbar__btn
  ) {
  transition:
    transform var(--hover-dur) var(--spring-snappy),
    box-shadow var(--hover-dur) var(--ease-apple);
}

/* Older rules also eased colours on these; keep that (plain colour fades, no layout properties) */
html :is(.proof-toggle-btn, .icon-btn, .unit-pill, .menu-tile) {
  transition:
    transform var(--hover-dur) var(--spring-snappy),
    box-shadow var(--hover-dur) var(--ease-apple),
    background-color 220ms var(--ease-apple),
    border-color 220ms var(--ease-apple),
    color 220ms var(--ease-apple);
}

html :is(.b-topbar__home, .sem-pick__btn, a.b-crumb) {
  transform-origin: left center;
}

/* Tab bar icons and labels move, not the tab cell (it has borders and an overflow clip) */
html .b-tab > :is(.b-ico, .b-tab__label),
html .b-topbar__logo {
  transition: transform var(--hover-dur) var(--spring-snappy);
}

html .pf-seg button {
  transition: opacity var(--press-dur) var(--ease-apple);
}

/* ── 4. Hover: only real pointers (never sticky on touch screens), and never under reduced motion ────────────────────────────────────── */
@media (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) {
  html :is(
      .b-card.is-link, .hm-subject, .ch-card.is-link, .tp-topic.is-link, .cal-item--exam,
      .subject-card, .menu-tile, .tool-tile,
      .b-cta, .tp-continue, .hm-next,
      .b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go, .proof-toggle-btn, .icon-btn, .btn-google,
      .search-result-option, .sem-pick__list li, .sem-pick__btn, a.b-pill, button.b-pill, a.b-chip, button.b-chip,
      .unit-pill, .btn-link, a.b-crumb, .b-topbar__home
    ):hover {
    transform: translateY(-2px) scale(1.025);
  }

  /* Icon-sized targets: 2.5 % of 48px is invisible, so they swell more */
  html .b-topbar__btn:hover {
    transform: translateY(-2px) scale(1.12);
  }

  html .b-tab:hover > .b-ico {
    transform: translateY(-2px) scale(1.12);
  }

  html .b-topbar__home:hover .b-topbar__logo {
    transform: rotate(-4deg) scale(1.06);
  }

  /* Hard shadows grow a little (about +2px of offset); same colours as the resting shadows */
  html :is(.b-card.is-link, .hm-subject, .ch-card.is-link, .cal-item--exam):hover {
    box-shadow: 9px 9px 0 var(--ink); /* resting 7px */
  }
  html .tp-topic.is-link:hover {
    box-shadow: 7px 7px 0 var(--ink); /* resting 5px */
  }
  html .b-cta:hover {
    box-shadow: 7px 7px 0 var(--fill-sand); /* resting 5px */
  }
  html .tp-continue:hover {
    box-shadow: 7px 7px 0 var(--subject-accent, var(--fill-sand, #e8d6b2));
  }
  html .hm-next:hover {
    box-shadow: 7px 7px 0 var(--fill-butter);
  }
  html :is(.b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go):hover {
    box-shadow: 6px 6px 0 var(--ink); /* resting 4px */
  }
  html .b-btn--solid:hover {
    box-shadow: 6px 6px 0 var(--fill-sand);
  }
}

/* Touch screens: the older un-gated :hover lifts (layout.css .icon-btn, components.css .menu-tile, .unit-pill,
   .proof-toggle-btn, .search-result-option) would stay stuck after a tap. Cancel the movement there. */
@media (hover: none), (pointer: coarse) {
  html :is(.icon-btn, .menu-tile, .unit-pill, .proof-toggle-btn, .search-result-option):hover:not(:active, .is-active) {
    transform: none;
  }
}

/* ── 5. Press: every input type (touch, pen, mouse, keyboard-driven :active) ─────────────────────────── */
/* Reconciliation with the older press rules (`translate(2px,2px)` / `translate(3px,3px)` in brutal.css,
   components.css, layout.css, screens-study.css, screens-syllabus.css, screens-calendar.css; tiles.css and
   subject-page.css are no longer linked): NOT edited. These rules out-rank them (the `html` prefix, and this
   file loads later) and replace the translate with scale(var(--press-scale)). The old rules' shadow collapse
   is repeated below value for value, so the pressed look is the old "sunk into the page" shadow plus the dip. */
html :is(
    .b-card.is-link, .hm-subject, .ch-card.is-link, .tp-topic.is-link, .cal-item--exam,
    .subject-card, .menu-tile, .tool-tile,
    .b-cta, .tp-continue, .hm-next,
    .b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go, .proof-toggle-btn, .icon-btn, .btn-google,
    .search-result-option, .sem-pick__list li, .sem-pick__btn, a.b-pill, button.b-pill, a.b-chip, button.b-chip,
    .unit-pill, .btn-link, a.b-crumb, .b-topbar__home
  ):active {
  transform: scale(var(--press-scale));
  transition-duration: var(--press-dur);
  transition-timing-function: var(--ease-apple);
}

html :is(.b-card.is-link, .hm-subject, .ch-card.is-link, .cal-item--exam):active {
  box-shadow: var(--shadow-sm, 4px 4px 0 var(--ink));
}
html .tp-topic.is-link:active {
  box-shadow: 3px 3px 0 var(--ink);
}
html .b-cta:active {
  box-shadow: 3px 3px 0 var(--fill-sand);
}
html .tp-continue:active {
  box-shadow: 3px 3px 0 var(--subject-accent, var(--fill-sand, #e8d6b2));
}
html .hm-next:active {
  box-shadow: 3px 3px 0 var(--fill-butter);
}
html :is(.b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go):active {
  box-shadow: 2px 2px 0 var(--ink);
}
html .b-btn--solid:active {
  box-shadow: 2px 2px 0 var(--fill-sand);
}

/* A button inside a tappable card (e.g. the PDF button on a syllabus card) is pressed on its own: the card
   behind it must not dip as well. */
html .b-card.is-link:has(button:active, .b-btn:active, .b-pill:active) {
  transform: none;
}

/* ── 6. Tab bar, top bar, segmented control ──────────────────────────────────────────────────────────── */
html .b-topbar__btn:active {
  transform: scale(var(--press-scale-sm));
  transition-duration: var(--press-dur);
  transition-timing-function: var(--ease-apple);
}

html .b-tab:active > .b-ico {
  transform: scale(0.86);
  transition-duration: var(--press-dur);
  transition-timing-function: var(--ease-apple);
}
html .b-tab:active > .b-tab__label {
  transform: scale(0.94);
  transition-duration: var(--press-dur);
  transition-timing-function: var(--ease-apple);
}
html .b-topbar__home:active .b-topbar__logo {
  transform: scale(var(--press-scale-sm));
  transition-duration: var(--press-dur);
  transition-timing-function: var(--ease-apple);
}

/* Segmented control sits inside an overflow-clipped, bordered track: scaling a segment would open gaps in
   it, so a press is a quick opacity dip instead. */
html .pf-seg button:active {
  opacity: 0.65;
}

/* ── 7. Frosted glass ────────────────────────────────────────────────────────────────────────────────── */
/* The colour is mixed in the rule itself (not stored in a token) so it re-resolves from whichever --paper
   (light, dark, desktop look) applies at that element. Without support the existing solid backgrounds stay (--paper on the bars, rgba scrims on the sheets).
   The older `backdrop-filter: blur(2px)` on .b-sheet-backdrop was removed from brutal.css so this is the
   only declaration; .modal-backdrop (components.css, a different class) is untouched. */
@supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) and
  (background: color-mix(in srgb, red 50%, transparent)) {
  .b-topbar,
  .b-tabbar {
    background: color-mix(in srgb, var(--paper) var(--glass-mix), transparent);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    backdrop-filter: blur(16px) saturate(180%);
  }

  .b-sheet-backdrop {
    background: var(--scrim);
    -webkit-backdrop-filter: blur(16px) saturate(180%);
    backdrop-filter: blur(16px) saturate(180%);
  }
}

/* ── 8. Concentric radii: inner radius = outer radius - padding (floored at --r-min) ─────────────────── */
.b-card {
  --r-outer: var(--radius, 7px);
  --pad: 16px;
}
.tp-topic,
.cr-q {
  --pad: 14px;
}
.b-cta,
.tp-continue,
.hm-next {
  --r-outer: var(--radius, 7px);
  --pad: 14px;
}
.search-result-option {
  --r-outer: var(--radius-sm, 6px);
  --pad: 10px;
}
.b-sheet {
  --r-outer: var(--radius, 7px);
  --pad: 20px;
}

:is(.b-card, .b-cta, .tp-continue, .hm-next, .search-result-option, .b-sheet)
  :is(.b-badge, .b-stamp, .b-chip, .b-btn, .b-pill, .tp-code, .search-kind-stamp) {
  border-radius: var(--r-min); /* fallback where max() is unsupported */
  border-radius: max(var(--r-min), calc(var(--r-outer) - var(--pad)));
}

/* ── 9. View transitions (progressive enhancement; the router is in js/app.js + js/view-transitions.js) ─── */
/* This is a hash-routed single-page app, so there is deliberately NO `@view-transition { navigation: auto }`
   here: that opts cross-document (multi-page) navigations in and does nothing for hash changes. */
::view-transition-group(root) {
  animation-duration: var(--vt-root-dur);
  animation-timing-function: var(--ease-apple);
}
::view-transition-old(root) {
  animation: vt-root-out var(--vt-root-dur) var(--ease-apple) both;
}
::view-transition-new(root) {
  animation: vt-root-in var(--vt-root-dur) var(--ease-apple) both;
}

@keyframes vt-root-out {
  to {
    opacity: 0;
    transform: translateY(-4px);
  }
}
@keyframes vt-root-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

/* Card -> page header morph. The JS names the tapped card and the destination header "hero" for the
   duration of the transition only. Crop (don't stretch) the two snapshots while the box changes size. */
::view-transition-group(hero) {
  animation-duration: var(--vt-hero-dur);
  animation-timing-function: var(--vt-hero-ease);
}
::view-transition-old(hero),
::view-transition-new(hero) {
  height: 100%;
  object-fit: cover;
  object-position: left top;
  overflow: clip;
}

/* ── 10. Reduced motion: no scaling or translating, instant state changes ────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  /* Hover rules above are already switched off by their media query. A press keeps its instant shadow
     collapse (it is not movement) but never scales; colour/opacity swaps are instant. */
  html :is(
      .b-card.is-link, .hm-subject, .ch-card.is-link, .tp-topic.is-link, .cal-item--exam,
      .subject-card, .menu-tile, .tool-tile,
      .b-cta, .tp-continue, .hm-next,
      .b-btn, .btn-primary, .btn-secondary, .tp-back, .cr-q__go, .proof-toggle-btn, .icon-btn, .btn-google,
      .search-result-option, .sem-pick__list li, .sem-pick__btn, a.b-pill, button.b-pill, a.b-chip, button.b-chip,
      .unit-pill, .btn-link, a.b-crumb, .b-topbar__home, .b-topbar__btn, .pf-seg button,
      .b-tab > .b-ico, .b-tab > .b-tab__label, .b-topbar__logo
    ) {
    transform: none !important;
    transition: none !important;
  }

  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation-duration: 0s !important;
    animation-delay: 0s !important;
  }
}
