/* ==========================================================================
   Study guide layer for generated slide breakdowns (CYS403).
   Loaded after each page's own <style>; paired with /javascripts/study-guide.js.
   Uses the page's theme tokens (--a…--f accents, --surface, --border, --text…)
   so it follows light and dark mode. Animations only run once the script adds
   .bdx-anim, and never with prefers-reduced-motion.
   ========================================================================== */

/* ---------- readability ---------- */
main > section {
  scroll-margin-top: 5rem;
}

main > section h2 {
  margin-bottom: 1.1rem;
}

main p,
main li {
  line-height: 1.75;
}

/* Use the room the screen gives: a wider column on laptops, slim gutters on phones
   (the page template's own main/hero rules cap at 1120px with fixed 2rem padding). */
main {
  max-width: 1320px;
  padding: clamp(2.25rem, 5vw, 4rem) clamp(1rem, 3.5vw, 2.5rem);
}

.hero {
  min-height: min(60vh, 560px);
  padding: clamp(3rem, 8vw, 5rem) clamp(1rem, 3.5vw, 2rem)
    clamp(2.5rem, 6vw, 4rem);
}

main ul {
  display: grid;
  gap: 0.4rem;
  margin: 0.5rem 0 1.1rem;
}

.bdx-sub {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 2.1rem;
  font-size: 1.12rem;
}

.bdx-sub::before {
  content: "";
  flex: 0 0 4px;
  height: 1.1em;
  border-radius: 4px;
  background: currentColor;
}

.bdx-icon {
  width: 18px;
  height: 18px;
  display: block;
}

/* ---------- tips ---------- */
.tip {
  border-left-width: 4px;
  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease;
}

.tip:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 30px var(--shadow);
}

.tip-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--surface2);
  margin-top: 0;
}

.tip-blue {
  border-left-color: var(--a);
}
.tip-blue .tip-icon {
  color: var(--a);
}
.tip-green {
  border-left-color: var(--d);
}
.tip-green .tip-icon {
  color: var(--d);
}
.tip-yellow {
  border-left-color: var(--f);
}
.tip-yellow .tip-icon {
  color: var(--f);
}
.tip-warn {
  border-left-color: var(--e);
}
.tip-warn .tip-icon {
  color: var(--e);
}

/* ---------- memory cards ---------- */
.bdx-memory {
  position: relative;
  overflow: hidden;
  padding: 1.4rem 1.5rem;
}

.bdx-memory::after {
  content: "";
  position: absolute;
  inset: auto -40px -60px auto;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  background: radial-gradient(
    circle,
    rgba(255, 77, 106, 0.16),
    transparent 70%
  );
  pointer-events: none;
}

.bdx-memory .mnemonic-icon {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  color: var(--c);
  background: rgba(255, 77, 106, 0.14);
}

.bdx-memory .mnemonic-icon .bdx-icon {
  width: 22px;
  height: 22px;
}

.bdx-memory .mnemonic-body {
  flex: 1;
  min-width: 0;
}

.bdx-memory .mnemonic-body h4 {
  margin-bottom: 0.85rem;
}

.bdx-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin: 0.1rem 0 0.9rem;
}

.bdx-tile {
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font:
    800 1.25rem "JetBrains Mono",
    monospace;
  color: #ffffff;
  background: linear-gradient(135deg, var(--c), var(--b));
  box-shadow: 0 6px 18px rgba(255, 77, 106, 0.28);
}

ul.bdx-pairs {
  list-style: none;
  display: grid;
  gap: 0.5rem;
  margin: 0.1rem 0 0.7rem;
  padding: 0;
}

ul.bdx-pairs li {
  display: grid;
  grid-template-columns: minmax(9rem, max-content) auto 1fr;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.85rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  line-height: 1.5;
}

ul.bdx-pairs li::before {
  content: none;
}

.bdx-term {
  font-weight: 700;
  color: var(--b);
}

.bdx-arrow {
  color: var(--text3);
}

.bdx-meaning {
  color: var(--text);
}

.bdx-note {
  margin: 0.35rem 0 0;
  color: var(--text2);
}

.bdx-chain {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  margin: 0.1rem 0 0.8rem;
}

.bdx-chip {
  padding: 0.4rem 0.85rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.88rem;
  color: var(--heading);
  background: var(--surface2);
  border: 1px solid rgba(255, 77, 106, 0.35);
}

.bdx-chain-sep {
  color: var(--c);
  font-weight: 700;
}

.bdx-phrase {
  margin: 0.1rem 0 0.75rem;
  padding: 0.6rem 0.9rem;
  border-radius: 10px;
  border-left: 3px solid var(--b);
  background: var(--surface);
  font-weight: 700;
  color: var(--heading);
}

@media (prefers-reduced-motion: no-preference) {
  .bdx-anim .bdx-memory.bdx-in .bdx-chip {
    animation: bdx-pop 0.55s cubic-bezier(0.3, 1.5, 0.5, 1) backwards;
    animation-delay: calc(250ms + var(--bdx-i, 0) * 110ms);
  }
}

/* ---------- steps as a timeline ---------- */
.bdx-timeline {
  position: relative;
  gap: 0.9rem;
}

.bdx-timeline::before {
  content: "";
  position: absolute;
  left: calc(1.25rem + 14px);
  top: 1.4rem;
  bottom: 1.4rem;
  width: 2px;
  border-radius: 2px;
  background: linear-gradient(var(--a), var(--c));
  opacity: 0.4;
}

.bdx-timeline .step {
  position: relative;
  background: var(--bg2);
  transition:
    transform 0.25s ease,
    border-color 0.25s ease,
    box-shadow 0.25s ease;
}

.bdx-timeline .step:hover {
  transform: translateX(4px);
  border-color: var(--border2);
  box-shadow: 0 10px 26px var(--shadow);
}

.bdx-timeline .step-num {
  position: relative;
  z-index: 1;
  border-radius: 50%;
  box-shadow: 0 0 0 4px var(--bg2);
}

/* ---------- flows, formulas, cards, tables ---------- */
.bdx-flow .flow-box {
  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease;
}

.bdx-flow .flow-box:hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 24px var(--shadow);
}

.bdx-flow .flow-arrow span {
  display: inline-block;
}

.bdx-formula {
  position: relative;
  white-space: normal;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 1.4rem;
  padding: 1.4rem 1.25rem 1rem;
  border-left: 3px solid var(--c);
}

.bdx-formula-label {
  position: absolute;
  top: -0.65rem;
  left: 1rem;
  padding: 0.1rem 0.55rem;
  border-radius: 6px;
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #ffffff;
  background: var(--c);
}

.bdx-line {
  display: block;
}

.def-card:hover {
  border-color: var(--border2);
  box-shadow: 0 14px 34px var(--shadow);
}

.tbl tbody tr:nth-child(even) td {
  background: var(--surface);
}

.tbl td:first-child {
  font-weight: 600;
  color: var(--heading);
}

.vs > .ex {
  transition:
    transform 0.25s ease,
    box-shadow 0.25s ease;
}

.vs > .ex:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 30px var(--shadow);
}

/* ---------- reading progress ---------- */
.bdx-progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 3px;
  z-index: 2000;
  pointer-events: none;
}

.bdx-bar .bdx-progress {
  position: absolute;
  inset: auto 0 -1px 0;
  height: 2px;
}

.bdx-progress > i {
  display: block;
  height: 100%;
  transform-origin: left;
  transform: scaleX(0);
  background: linear-gradient(90deg, var(--a), var(--c), var(--b));
}

/* ---------- page header: contents, search, theme ---------- */
/* html-theme-sync.js sets overflow-x: hidden on html and body, which makes body a
   scroll container and stops the sticky header; clip hides overflow without that. */
html:not(#bdx),
body:not(#bdx) {
  overflow-x: clip;
}

.bdx-bar {
  position: sticky;
  top: 0;
  z-index: 1500;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

.bdx-bar-inner {
  max-width: 1320px;
  margin: 0 auto;
  padding: 0.6rem clamp(1rem, 3.5vw, 2.5rem);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.bdx-bar-title {
  margin-right: auto;
  color: var(--heading);
  font:
    700 0.74rem "JetBrains Mono",
    monospace;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-decoration: none;
  white-space: nowrap;
}

.bdx-bar-title span {
  color: var(--c);
}

.bdx-bar-search {
  display: flex;
  min-width: 0;
}

.bdx-bar-search .sg-page-search.sg-page-search {
  padding: 0.2rem;
}

.bdx-bar-search .sg-page-search__input.sg-page-search__input {
  width: clamp(8rem, 22vw, 15rem);
  height: 1.9rem;
}

.bdx-bar-search .sg-page-search__button.sg-page-search__button {
  width: 1.9rem;
  height: 1.9rem;
}

.bdx-bar .theme-toggle {
  position: static;
  flex: none;
  padding: 0.4rem 0.85rem;
}

.bdx-bar .theme-toggle:hover {
  transform: none;
}

.bdx-toc {
  position: relative;
  flex: none;
}

.bdx-toc-toggle {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--border2);
  background: var(--toggle-bg, var(--surface2));
  color: var(--heading);
  font:
    700 0.72rem "JetBrains Mono",
    monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

.bdx-toc-toggle:hover,
.bdx-toc.is-open .bdx-toc-toggle {
  border-color: var(--c);
}

.bdx-toc-toggle svg {
  width: 16px;
  height: 16px;
}

.bdx-toc-count {
  color: var(--c);
}

.bdx-toc-list {
  position: absolute;
  right: 0;
  top: calc(100% + 0.6rem);
  width: min(360px, calc(100vw - 2rem));
  max-height: min(70vh, 560px);
  overflow: auto;
  margin: 0;
  padding: 0.5rem;
  list-style: none;
  border-radius: 14px;
  border: 1px solid var(--border2);
  background: var(--bg2);
  box-shadow: 0 18px 50px var(--shadow);
  opacity: 0;
  transform: translateY(-8px) scale(0.98);
  transform-origin: top right;
  pointer-events: none;
  transition:
    opacity 0.2s ease,
    transform 0.2s ease;
}

.bdx-toc.is-open .bdx-toc-list {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

.bdx-toc-list a {
  display: flex;
  gap: 0.7rem;
  padding: 0.55rem 0.65rem;
  border-radius: 9px;
  color: var(--text2);
  text-decoration: none;
  font:
    500 0.8rem/1.35 "Space Grotesk",
    sans-serif;
}

.bdx-toc-list a:hover {
  background: var(--surface2);
  color: var(--heading);
}

.bdx-toc-list a.is-active {
  background: rgba(255, 77, 106, 0.12);
  color: var(--heading);
}

.bdx-toc-num {
  min-width: 1.6rem;
  font-family: "JetBrains Mono", monospace;
  color: var(--text3);
}

.bdx-toc-list a.is-active .bdx-toc-num {
  color: var(--c);
}

/* ---------- slide figures ---------- */
.bdx-figure {
  margin: 1.6rem 0;
}

.bdx-figure-zoom {
  display: block;
  width: 100%;
  padding: 1rem;
  border: 1px solid var(--border2);
  border-radius: 14px;
  background: #fff;
  cursor: zoom-in;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.bdx-figure-zoom:hover,
.bdx-figure-zoom:focus-visible {
  border-color: var(--c);
  box-shadow: 0 10px 30px var(--shadow);
}

/* No width: auto here. The width/height attributes must size the box before a lazy
   image loads; otherwise it starts at zero height and every figure shoves the page
   down as it appears, so contents jumps and search results land in the wrong place. */
.bdx-figure img {
  display: block;
  max-width: 100%;
  height: auto;
  max-height: min(75vh, 760px);
  object-fit: contain;
  margin: 0 auto;
}

.bdx-figure figcaption {
  display: flex;
  gap: 0.7rem;
  align-items: baseline;
  margin-top: 0.6rem;
  color: var(--text2);
  font-size: 0.88rem;
  line-height: 1.55;
}

/* The tag links to the slide the diagram came from. */
.bdx-figure-tag {
  flex: none;
  color: var(--c);
  font:
    700 0.68rem "JetBrains Mono",
    monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-decoration: none;
  border-bottom: 1px solid transparent;
}

a.bdx-figure-tag:hover,
a.bdx-figure-tag:focus-visible {
  border-bottom-color: currentColor;
}

.bdx-lightbox {
  width: fit-content;
  max-width: 96vw;
  max-height: 94vh;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: #fff;
  overflow: auto;
}

.bdx-lightbox::backdrop {
  background: rgba(4, 7, 14, 0.82);
}

/* Never upscale past the crop's own pixels; small slide images blur. */
.bdx-lightbox img {
  display: block;
  max-width: 100%;
  height: auto;
  padding: 1.25rem;
}

.bdx-lightbox-close {
  position: sticky;
  top: 0.6rem;
  float: right;
  margin: 0.6rem;
  padding: 0.35rem 0.8rem;
  border: 1px solid rgba(15, 23, 42, 0.2);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  color: #0f172a;
  font:
    700 0.72rem "JetBrains Mono",
    monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}

/* ---------- flashcards ---------- */
.bdx-deck-bar {
  display: flex;
  justify-content: flex-end;
  margin: 0.4rem 0 1rem;
}

.bdx-shuffle {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.95rem;
  border-radius: 999px;
  border: 1px solid var(--border2);
  background: var(--surface2);
  color: var(--heading);
  font:
    600 0.8rem "Space Grotesk",
    sans-serif;
  cursor: pointer;
  transition:
    transform 0.2s ease,
    border-color 0.2s ease;
}

.bdx-shuffle:hover {
  transform: translateY(-2px);
  border-color: var(--c);
}

.bdx-shuffle svg {
  width: 15px;
  height: 15px;
}

.bdx-deck {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 0.9rem;
}

.bdx-card {
  position: relative;
  display: block;
  min-height: 180px;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  perspective: 1000px;
}

.bdx-card-inner {
  position: relative;
  display: block;
  min-height: 180px;
  transform-style: preserve-3d;
  transition: transform 0.6s cubic-bezier(0.3, 0.8, 0.3, 1);
}

.bdx-card.is-flipped .bdx-card-inner {
  transform: rotateY(180deg);
}

.bdx-card-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.5rem;
  padding: 1.1rem 1.2rem;
  border-radius: 14px;
  border: 1px solid var(--border2);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  overflow: auto;
  transition:
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.bdx-front {
  background: linear-gradient(145deg, var(--bg2), var(--bg3));
}

.bdx-back {
  transform: rotateY(180deg);
  background:
    linear-gradient(
      145deg,
      rgba(255, 77, 106, 0.16),
      rgba(110, 231, 247, 0.08)
    ),
    var(--bg2);
  color: var(--text);
  font-size: 0.88rem;
  line-height: 1.55;
}

.bdx-card:hover .bdx-card-face {
  border-color: var(--c);
  box-shadow: 0 12px 30px var(--shadow);
}

.bdx-card:focus-visible {
  outline: none;
}

.bdx-card:focus-visible .bdx-card-face {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

.bdx-card-kicker {
  font:
    700 0.62rem "JetBrains Mono",
    monospace;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--c);
}

.bdx-card-topic {
  font:
    700 1.05rem/1.3 "Space Grotesk",
    sans-serif;
  color: var(--heading);
}

.bdx-card-hint {
  margin-top: auto;
  font-size: 0.7rem;
  color: var(--text3);
}

/* ---------- motion ---------- */
@keyframes bdx-rise {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes bdx-pop {
  0% {
    opacity: 0;
    transform: scale(0.4) rotate(-12deg);
  }
  70% {
    opacity: 1;
    transform: scale(1.12) rotate(3deg);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(0);
  }
}

@keyframes bdx-nudge {
  0%,
  100% {
    transform: translateX(0);
    opacity: 0.55;
  }
  50% {
    transform: translateX(5px);
    opacity: 1;
  }
}

@keyframes bdx-shine {
  to {
    background-position: 200% center;
  }
}

@keyframes bdx-bob {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(6px);
  }
}

@media (prefers-reduced-motion: no-preference) {
  .hero > :not(.hero-grid) {
    animation: bdx-rise 0.8s cubic-bezier(0.2, 0.7, 0.2, 1) backwards;
  }

  .hero h1 {
    animation-delay: 0.08s;
  }
  .hero .hero-sub {
    animation-delay: 0.18s;
  }
  .hero .hero-chips {
    animation-delay: 0.28s;
  }

  .hero .scroll-hint {
    animation:
      bdx-rise 0.8s 0.4s backwards,
      bdx-bob 2.4s 1.4s ease-in-out infinite;
  }

  .hero .chip {
    transition: transform 0.25s ease;
  }

  .hero .chip:hover {
    transform: translateY(-3px);
  }

  .hero .gradient-word {
    background-size: 200% auto;
    animation: bdx-shine 8s linear infinite;
  }

  .bdx-flow .flow-arrow span {
    animation: bdx-nudge 1.8s ease-in-out infinite;
  }

  /* Scroll reveal: study-guide.js marks below-the-fold elements .bdx-wait and
     swaps it for .bdx-in as they scroll into view; content on screen at load
     never hides. Fill mode "backwards" hands control back to hover transforms. */
  .bdx-anim .bdx-wait {
    opacity: 0;
  }

  .bdx-anim .bdx-in {
    animation: bdx-rise 0.65s cubic-bezier(0.2, 0.7, 0.2, 1) backwards;
    animation-delay: calc(var(--bdx-i, 0) * 70ms);
  }

  main > section .sec-line {
    transform-origin: left;
    transition: transform 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) 0.15s;
  }

  .bdx-anim main > section.bdx-wait .sec-line {
    transform: scaleX(0);
  }

  .bdx-anim .bdx-memory.bdx-in .bdx-tile {
    animation: bdx-pop 0.55s cubic-bezier(0.3, 1.5, 0.5, 1) backwards;
    animation-delay: calc(250ms + var(--bdx-i, 0) * 90ms);
  }
}

@media (prefers-reduced-motion: reduce) {
  .bdx-card-inner,
  .bdx-toc-list {
    transition: none;
  }
}

/* ---------- small screens + print ---------- */
@media (max-width: 600px) {
  ul.bdx-pairs li {
    grid-template-columns: 1fr;
    gap: 0.2rem;
  }

  .bdx-arrow {
    display: none;
  }

  .bdx-bar-inner {
    flex-wrap: wrap;
    padding: 0.55rem 1rem;
    gap: 0.5rem;
  }

  .bdx-bar-search {
    order: 5;
    flex: 1 0 100%;
  }

  .bdx-bar-search .sg-page-search.sg-page-search {
    flex: 1;
  }

  .bdx-bar-search .sg-page-search__input.sg-page-search__input {
    flex: 1;
    width: auto;
  }

  .bdx-toc-toggle .bdx-toc-count,
  .bdx-bar .theme-toggle #toggle-label {
    display: none;
  }

  .bdx-figure-zoom {
    padding: 0.5rem;
  }
}

/* Narrow phones and the phone-width wrapper iframe: keep the header on one row
   above the search by dropping the "Chapter N" half of the title. */
@media (max-width: 420px) {
  .bdx-bar-title span {
    display: none;
  }
}

@media print {
  .bdx-bar,
  .bdx-progress,
  .bdx-deck-bar {
    display: none !important;
  }

  .bdx-card-inner {
    transform: none !important;
  }

  .bdx-card-face {
    position: static;
  }

  .bdx-back {
    transform: none;
  }
}
