/*
 * The home page (iOS-437). Direction and the reason behind each choice:
 * DESIGN.md. A pinned opening whose middle panel grows as you scroll, then one
 * reading column in Plex Serif, in five numbered chapters.
 */

@font-face {
  font-family: "IBM Plex Serif";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/ibm-plex-serif-latin-400-normal.woff2") format("woff2");
}

@font-face {
  font-family: "IBM Plex Serif";
  font-style: normal;
  font-weight: 600;
  font-display: swap;
  src: url("fonts/ibm-plex-serif-latin-600-normal.woff2") format("woff2");
}

:root {
  --font-serif: "IBM Plex Serif", ui-serif, Georgia, "Times New Roman", serif;
}

/* --- Header --------------------------------------------------------------
 * Floats over the opening instead of pushing it down, and tucks away while
 * reading down (assets/home.js). Focus inside it brings it back, so a keyboard
 * user never tabs into a bar that is off screen.
 */

.site-header {
  position: fixed;
  inset: 0 0 auto;
  transition: transform 250ms ease;
}

.site-header.is-tucked:not(:focus-within) { transform: translateY(-100%); }

/* --- Opening -------------------------------------------------------------
 * Three panels of art drawn in CSS. Their colours are fixed rather than
 * themed: they are a picture, and a picture does not flip in dark mode. Every
 * colour is a palette value or a darker step of one.
 *
 * Driven by three numbers from assets/home.js: --z (the middle panel's zoom),
 * --t (the title and contents), --i (the "Introducing" line). The defaults are
 * the finished state, which is what a visitor with no JavaScript or with
 * reduced motion sees.
 */

.opening {
  --z: 1;
  --t: 1;
  --i: 0;
  --horizon: 63%;
  /* How far the middle grows (42% of the width to 86%), and how far each side
   * moves out to make room, in its own widths. */
  --grow: 1.05;
  --shift: 75.9%;
  /* Horizon curve: the ellipse's width against the panel's. */
  --curve: 210%;
  --cream: #F4EFE6;
  position: relative;
  height: 100vh;
  height: 100svh;
  min-height: 20rem;
  background: #1A120C;
  color: var(--cream);
}

/* Pinned for one screen of scrolling. `.motion` is set in the page's <head>
 * only when motion is allowed, so the first paint is already the start state. */
.motion .opening {
  --z: 0;
  --t: 0;
  --i: 1;
  height: 200vh;
  height: 200svh;
}

.opening__stage {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100svh;
  min-height: 20rem;
  overflow: hidden;
}

.opening__art { position: absolute; inset: 0; display: flex; }

.panel { position: relative; height: 100%; }

.panel__lower { position: absolute; inset: 58% 0 0; }

/* Left: a ledger page, ruled, with its red double margin, torn above cream
 * paper. The book a budget used to live in. */
.panel--ledger {
  flex: 0 0 29%;
  transform: translateX(calc(var(--z) * -1 * var(--shift)));
  clip-path: polygon(0% 0%, 97.3% 0%, 99.69% 5.56%, 98.05% 11.11%, 97.85% 16.67%, 99.2% 22.22%, 98.69% 27.78%, 98.16% 33.33%, 99.26% 38.89%, 99.26% 44.44%, 98.71% 50%, 99.08% 55.56%, 98.95% 61.11%, 99.42% 66.67%, 97.93% 72.22%, 98.27% 77.78%, 99.14% 83.33%, 99.11% 88.89%, 97.44% 94.44%, 99.08% 100%, 0% 100%);
  background:
    linear-gradient(to right,
      transparent 16%, rgb(192 78 16 / 0.75) 16% calc(16% + 1px),
      transparent calc(16% + 1px) calc(16% + 4px),
      rgb(192 78 16 / 0.75) calc(16% + 4px) calc(16% + 5px), transparent 0),
    repeating-linear-gradient(to bottom, transparent 0 27px, rgb(42 31 20 / 0.28) 27px 28px),
    #F2A44A;
}

.panel--ledger .panel__lower {
  clip-path: polygon(0% 4.69%, 7.14% 2.79%, 14.29% 3.65%, 21.43% 4.39%, 28.57% 0.29%, 35.71% 1.37%, 42.86% 2.8%, 50% 2.54%, 57.14% 2.11%, 64.29% 0.96%, 71.43% 4.54%, 78.57% 0.27%, 85.71% 1.83%, 92.86% 1.72%, 100% 3.32%, 100% 100%, 0% 100%);
  background:
    repeating-radial-gradient(ellipse 150% 70% at 15% 115%,
      transparent 0 11px, rgb(42 31 20 / 0.55) 12px 14px, transparent 15px 23px),
    var(--cream);
}

/* Middle: first light over a dark horizon, the planet-edge curve drawn as one
 * large ellipse. It grows around the horizon line, so the horizon stays put
 * and the title can sit just above it. */
.panel--dawn {
  flex: 0 0 42%;
  z-index: 1;
  transform-origin: 50% var(--horizon);
  transform: scale(calc(1 + var(--grow) * var(--z)));
  /* Ellipse centre 87% below the horizon, with the rim at its 87% stop, so the
   * painted horizon lands exactly on --horizon. */
  background:
    radial-gradient(ellipse var(--curve) 100% at 50% calc(var(--horizon) + 87%),
      #1A120C 0 86.6%, #C04E10 87%, #E8601A 87.5%,
      rgb(242 164 74 / 0.5) 88.2%, transparent 90%),
    linear-gradient(to bottom,
      #12161C 0%, #1C2835 28%, #2C4660 46%, #46657F 56%, #5E7486 61%);
}

/* Right: dark slate above, and below it the reeded edge of a stack of coins. */
.panel--reeded {
  flex: 0 0 29%;
  transform: translateX(calc(var(--z) * var(--shift)));
  clip-path: polygon(100% 100%, 100% 0%, 1.32% 0%, 2.5% 5.56%, 2.2% 11.11%, 0.42% 16.67%, 1.43% 22.22%, 2.5% 27.78%, 1.89% 33.33%, 1.77% 38.89%, 1.6% 44.44%, 2.47% 50%, 2.46% 55.56%, 1.07% 61.11%, 0.97% 66.67%, 2.28% 72.22%, 0.96% 77.78%, 0.5% 83.33%, 0.87% 88.89%, 1.21% 94.44%, 0.87% 100%);
  background: #2E2C2A;
}

.panel--reeded .panel__lower {
  inset-block-start: 55%;
  clip-path: polygon(0% 5.55%, 8.33% 3.09%, 16.67% 0.99%, 25% 2.43%, 33.33% 1.7%, 41.67% 2.19%, 50% 2.54%, 58.33% 2%, 66.67% 3.19%, 75% 3.68%, 83.33% 1.4%, 91.67% 5.44%, 100% 2.75%, 100% 100%, 0% 100%);
  background:
    linear-gradient(to bottom, rgb(255 236 214 / 0.35), transparent 22% 70%, rgb(26 18 12 / 0.5)),
    repeating-linear-gradient(to right, #E8601A 0 10px, #C04E10 10px 13px, #F2A44A 13px 14px);
}

/* Paper grain over the whole picture: one static tile, so nothing is
 * recomputed while the panels move. */
.opening__stage::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .6 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  opacity: 0.4;
  mix-blend-mode: overlay;
}

.opening__intro,
.opening__hint,
.opening__head,
.contents {
  position: absolute;
  z-index: 3;
  margin: 0;
  text-align: center;
}

.opening__intro {
  inset-inline: 0;
  top: 46%;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: clamp(1.75rem, 1.2rem + 2.2vw, 2.75rem);
  letter-spacing: -0.015em;
  max-width: none;
  opacity: var(--i);
}

.opening__hint {
  inset-inline: 0;
  bottom: 1.25rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  max-width: none;
  opacity: var(--i);
}

/* The title sits above the horizon's glow, the contents just below the
 * horizon, so the small text always lands on the dark ground. Worst case for
 * the title is cream over the sky's lowest blue: 4.25:1, large text. */
.opening__head {
  inset-inline: var(--gutter);
  bottom: calc(100% - var(--horizon) + 4rem);
  opacity: var(--t);
  transform: translateY(calc((1 - var(--t)) * 1.25rem));
}

.opening__kicker {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin: 0 auto var(--space-sm);
}

.opening h1 {
  font-family: var(--font-serif);
  font-size: clamp(3.5rem, 1.6rem + 8vw, 8rem);
  line-height: 1;
  letter-spacing: -0.03em;
  margin: 0;
  text-shadow: 0 2px 28px rgb(18 22 28 / 0.45);
}

.contents {
  top: calc(var(--horizon) + 1.5rem);
  left: 50%;
  width: min(21rem, 100% - 2 * var(--gutter));
  font-family: var(--font-serif);
  font-size: 0.9375rem;
  opacity: var(--t);
  transform: translate(-50%, calc((1 - var(--t)) * 1.25rem));
}

/* Before the title has faded in, the links stay out of the pointer's way, so a
 * tap on the dark opening can't land on one nobody can see. Tabbing into the
 * opening shows it finished: !important beats the script's inline values. */
.motion .opening:not(.is-titled) .contents { pointer-events: none; }

.opening:focus-within {
  --z: 1 !important;
  --t: 1 !important;
  --i: 0 !important;
}

.motion .opening:focus-within .contents { pointer-events: auto; }

.contents ol { list-style: none; margin: 0; padding: 0; }

.contents a {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  min-height: 2.75rem;
  color: var(--cream);
  text-decoration: none;
}

.contents__n { font-family: var(--font-mono); font-size: 0.75rem; }

.contents__leader {
  flex: 1;
  border-bottom: 1px dashed rgb(244 239 230 / 0.45);
}

.contents a:hover .contents__label { text-decoration: underline; text-underline-offset: 0.2em; }
.contents a:hover .contents__leader { border-bottom-color: var(--cream); }

/* On a narrow screen the contents list is wider than 86% of the width, so the
 * middle grows to the full width and the sides leave entirely. The flatter
 * curve keeps the horizon from dropping under the ends of the first row. */
@media (max-width: 40rem) {
  .opening { --grow: 1.381; --shift: 100%; --curve: 420%; }
  .opening__intro { font-size: clamp(1.375rem, 1rem + 2.2vw, 1.75rem); }
}

/* Short screens (a landscape phone, a 568px iPhone SE) can't fit five 44px rows
 * under a horizon at 63%. A higher horizon and tighter rows keep all five on
 * screen; 2.25rem is still well past the 24px WCAG target minimum. */
@media (max-height: 38rem) {
  .opening { --horizon: 56%; }
  .contents { top: calc(var(--horizon) + 0.75rem); }
  .contents a { min-height: 2.25rem; }
  .opening__head { bottom: calc(100% - var(--horizon) + 1.5rem); }
}

/* A landscape phone (about 20rem tall) still can't fit five rows; it has the
 * width for two columns of three. */
@media (max-height: 28rem) and (min-width: 40rem) {
  .contents { width: min(38rem, 100% - 2 * var(--gutter)); }
  .contents ol { columns: 2; column-gap: var(--space-xl); }
  .contents li { break-inside: avoid; }
}

/* --- The reading column -------------------------------------------------- */

.story {
  font-family: var(--font-serif);
  font-size: clamp(1.0625rem, 1rem + 0.3vw, 1.1875rem);
  line-height: 1.65;
  padding-block: var(--space-4xl) var(--space-2xl);
}

/* Text in the middle column, figures allowed out to the wider track. */
.chapter {
  display: grid;
  grid-template-columns:
    [full-start] minmax(var(--gutter), 1fr)
    [text-start] minmax(0, 36rem) [text-end]
    minmax(var(--gutter), 1fr) [full-end];
  /* Clear the header on a #fragment jump. It tucks away on the way down, but
   * a jump upwards brings it back, and it is 3.75rem tall. */
  scroll-margin-top: 5rem;
}

/* Below about 384px the nav wraps and the header grows to two rows. */
@media (max-width: 24rem) {
  .chapter { scroll-margin-top: 6.5rem; }
}

/* Under 20rem (large text on a phone) it reaches four rows, 11.8rem. */
@media (width < 20rem) {
  .chapter { scroll-margin-top: 12.5rem; }
}

.chapter > * { grid-column: text; }
.chapter + .chapter { margin-top: var(--space-4xl); }

.story p { max-width: none; }

.story h2 {
  font-weight: 400;
  font-size: clamp(1.875rem, 1.35rem + 2vw, 2.75rem);
  letter-spacing: -0.02em;
  margin-bottom: var(--space-lg);
}

.story h3 {
  font-size: clamp(1.1875rem, 1.1rem + 0.4vw, 1.375rem);
  margin: var(--space-2xl) 0 var(--space-sm);
}

.story h2 + h3 { margin-top: 0; }

.story__lede {
  font-size: clamp(1.4375rem, 1.1rem + 1.4vw, 2rem);
  line-height: 1.3;
  letter-spacing: -0.01em;
}

.story__note {
  font-family: var(--font-sans);
  font-size: 0.875rem;
  color: var(--text-secondary);
}

/* Facts: definition lists, read top to bottom like the rest of the column. */
.facts { margin: 0 0 var(--space-lg); }
.facts dt { font-weight: 600; }
.facts dd { margin: 0 0 var(--space-md); color: var(--text-secondary); }

.lead-in { margin-bottom: var(--space-md); }
.lead-in strong { font-weight: 600; }

/* --- Figures --------------------------------------------------------------
 * The demo, the chart and the table step out of the text column, the way a
 * printed figure does, so each reads as a thing to look at rather than prose.
 */

.chapter > .figure {
  grid-column: full;
  width: 100%;
  max-width: 50rem;
  margin: var(--space-2xl) auto;
  padding-inline: var(--gutter);
}

.figure__frame {
  border: 1px solid var(--border);
  border-radius: var(--radius-surface);
  background: var(--bg-secondary);
  padding: var(--space-lg) clamp(var(--space-md), 3vw, var(--space-xl));
}

.figure__title {
  font-family: var(--font-sans);
  font-weight: 600;
  margin: 0;
}

.figure__note {
  font-family: var(--font-sans);
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin: 0 0 var(--space-lg);
}

/* The demo: the phone set in the app's own system font (SF Pro on Apple
 * devices), since the page around it is serif. */
#demo { font-family: var(--font-app); line-height: 1.3; }
.figure__reset { text-align: center; margin: var(--space-md) 0 0; }

/* --- Waterfall ------------------------------------------------------------
 * Pay comes in, each deduction comes off where the running total has got to,
 * and what is left is the one bar in the accent. Bars draw once on entry:
 * deductions grow from the right, as money taken away.
 */

.waterfall__rows { margin: 0; display: grid; gap: var(--space-sm); }

.waterfall__row {
  display: grid;
  grid-template-columns: minmax(4.5rem, 7rem) minmax(0, 1fr);
  align-items: center;
  gap: var(--space-sm);
  font-family: var(--font-sans);
  font-size: 0.9375rem;
}

/* Large text on a phone: each label goes above its bar. A media query's rem is
 * the browser's default text size, and zoom narrows the viewport instead, so
 * either one trips this; at the default size it starts below 272px. */
@media (max-width: 17rem) {
  .waterfall__row { grid-template-columns: minmax(0, 1fr); row-gap: var(--space-2xs); }
}

.waterfall__row dt { color: var(--text-secondary); }
.waterfall__row dd { margin: 0; display: flex; align-items: center; gap: var(--space-sm); }

.waterfall__track { position: relative; flex: 1; height: 1.25rem; }

.waterfall__bar {
  position: absolute;
  inset-block: 0;
  left: calc(var(--from) * 1%);
  width: calc(var(--size) * 1%);
  border-radius: 3px;
  background: var(--text-secondary);
  transform-origin: right;
}

.waterfall__row--in .waterfall__bar { background: var(--text-primary); transform-origin: left; }

.waterfall__amount {
  flex: 0 0 4.75rem;
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* The result row borrows the readout: mango tint, mono figure in the accent. */
.waterfall__row--left {
  margin-top: var(--space-xs);
  padding: var(--space-sm);
  margin-inline: calc(-1 * var(--space-sm));
  border-radius: var(--radius-control);
  background: var(--accent-primary-tint);
}

.waterfall__row--left dt { color: var(--text-primary); font-weight: 600; }
/* --accent-text, not --accent-primary: Mango on the tint is 2.93:1 in light
 * mode, under the 3:1 a bar needs; this is 4.98:1 light, 6.15:1 dark. */
.waterfall__row--left .waterfall__bar { background: var(--accent-text); transform-origin: left; }
.waterfall__row--left .waterfall__amount { color: var(--accent-text); font-weight: 500; }

@media (prefers-reduced-motion: no-preference) {
  .js-reveal .waterfall__bar {
    transform: scaleX(0);
    transition: transform 700ms cubic-bezier(0.2, 0.7, 0.2, 1) calc(var(--step) * 180ms);
  }
  .js-reveal .waterfall__amount {
    opacity: 0;
    transition: opacity 300ms ease calc(var(--step) * 180ms + 400ms);
  }
  .js-reveal .waterfall.is-visible .waterfall__bar { transform: none; }
  .js-reveal .waterfall.is-visible .waterfall__amount { opacity: 1; }
}

/* --- Free and Pro ---------------------------------------------------------
 * One table, two columns, because there are two tiers. Pro is outlined in the
 * accent because it is the one thing on the page that costs money.
 */

/* Relative, so the table's visually hidden "Included" labels scroll inside the
 * frame instead of widening the page when large text makes the table scroll. */
.figure__frame--table { padding: 0; overflow-x: auto; position: relative; }

.plans {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-family: var(--font-sans);
  font-size: 0.9375rem;
}

.plans th,
.plans td {
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.plans tbody tr:last-child > * { border-bottom: 0; }
.plans tbody th { font-weight: 400; }
.plans thead th { font-weight: 600; }
.plans td { width: 6.5rem; }

.plans :is(th, td):nth-child(3) {
  background: var(--accent-primary-tint);
  border-inline: 1.5px solid var(--accent-primary);
}

.plans thead > tr > :nth-child(3) { border-top: 1.5px solid var(--accent-primary); border-top-right-radius: var(--radius-surface); }
.plans tbody tr:last-child > :nth-child(3) { border-bottom: 1.5px solid var(--accent-primary); border-bottom-right-radius: var(--radius-surface); }

.plans .yes { font-weight: 600; }

/* --- Section ladder -------------------------------------------------------
 * Wide screens only, where the left margin has room for it. One tick per
 * chapter, the current one longer. A mouse shortcut: the contents list in the
 * opening is the accessible version, so this one is hidden from assistive tech.
 */

.ladder { display: none; }

@media (min-width: 75rem) {
  .ladder {
    position: fixed;
    left: 1.25rem;
    top: 50%;
    z-index: 40;
    display: flex;
    flex-direction: column;
    transform: translateY(-50%);
    opacity: 0;
    pointer-events: none;
    transition: opacity 300ms ease;
  }

  .is-reading .ladder { opacity: 1; pointer-events: auto; }

  .ladder a { display: block; width: 2rem; padding-block: 0.375rem; }

  .ladder a::before {
    content: "";
    display: block;
    width: 0.75rem;
    height: 1px;
    background: var(--text-secondary);
    transition: width 200ms ease;
  }

  .ladder a:hover::before,
  .ladder a.is-current::before { width: 1.5rem; height: 2px; background: var(--text-primary); }
}
