/* ============================================================
   Mobile layer.

   The design exports a fixed desktop layout as inline style
   attributes, which cannot carry media queries. responsive.py
   tags layout elements with r-* classes at build time; the rules
   below override them at narrow widths. Inline styles outrank a
   stylesheet, so these overrides need !important.

   Breakpoints: 900px collapses the two-column sections,
   640px switches to the phone layout.
   ============================================================ */

/* Nothing should ever force the page sideways. */
img, svg, video { max-width: 100%; height: auto; }

/* ---------- Tablet ---------- */
@media (max-width: 900px) {
  .r-2col { grid-template-columns: minmax(0, 1fr) !important; }
}

/* ---------- Phone ---------- */
@media (max-width: 640px) {

  /* Side gutters: 28px of desktop padding is too much of a 390px screen. */
  .r-shell,
  .r-pad { padding-left: 16px !important; padding-right: 16px !important; }

  /* Keep auto-fit tracks inside the screen: min(100%, n) stops a 300px
     minimum forcing a 300px column onto a 320px phone. */
  .r-af180 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)) !important; }
  .r-af200 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)) !important; }
  .r-af240 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)) !important; }
  .r-af260 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr)) !important; }
  .r-af280 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)) !important; }
  .r-af300 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)) !important; }
  .r-af320 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)) !important; }

  /* Let rows reflow instead of overflowing, and long strings break. */
  .r-wrapflex { flex-wrap: wrap !important; }
  .r-nowrap { white-space: normal !important; }

  /* Sticky header: brand and account on one row, sections scrolling beneath. */
  .r-topbar {
    padding-left: 16px !important;
    padding-right: 16px !important;
    gap: 10px 12px !important;
  }
  /* The bar is sticky, so every row it takes costs screen on every scroll:
     tighten it and let the brand shrink so it can share a row with the account. */
  .r-topbar { padding-top: 10px !important; padding-bottom: 10px !important; }
  .r-topbar > :first-child { gap: 8px !important; }
  .r-topbar > :first-child > span { font-size: 14px !important; }
  .r-topbar > :last-child { gap: 8px !important; }
  .r-topbar nav .r-tap { padding: 9px 11px !important; }

  .r-topbar > nav {
    order: 3;
    width: 100%;
    flex-wrap: nowrap !important;   /* scroll the sections, don't stack them */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .r-topbar > nav::-webkit-scrollbar { display: none; }
  .r-topbar > :last-child { order: 2; margin-left: auto !important; }
  /* Section names must stay on one line inside the scrolling strip. */
  .r-topbar .r-nowrap { white-space: nowrap !important; }

  /* The plan table stops being a table and becomes a labelled card.
     Six columns need ~444px before gaps, which no phone has. */
  .r-rowhead { display: none !important; }
  .r-row {
    grid-template-columns: 24px repeat(3, minmax(0, 1fr)) !important;
    gap: 12px 10px !important;
    padding: 14px 16px !important;
    align-items: start !important;
  }
  .r-row > :nth-child(1) { grid-area: 1 / 1 / 2 / 2; }   /* step number */
  .r-row > :nth-child(2) { grid-area: 1 / 2 / 2 / 5; }   /* measure + note */
  .r-row > :nth-child(3) { grid-area: 2 / 2 / 3 / 3; }   /* cost */
  .r-row > :nth-child(4) { grid-area: 2 / 3 / 3 / 4; }   /* saving */
  .r-row > :nth-child(5) { grid-area: 2 / 4 / 3 / 5; }   /* carbon */
  .r-row > :nth-child(6) { grid-area: 3 / 1 / 4 / 5; justify-self: start; }

  /* The column headings are hidden above, so each figure carries its own. */
  .r-row > :nth-child(3)::before { content: "Cost to fit"; }
  .r-row > :nth-child(4)::before { content: "Saves"; }
  .r-row > :nth-child(5)::before { content: "Carbon"; }
  .r-row > :nth-child(3)::before,
  .r-row > :nth-child(4)::before,
  .r-row > :nth-child(5)::before {
    display: block;
    margin-bottom: 3px;
    font-size: 10px;
    font-weight: var(--kc-weight-emphasis);
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--kc-ink-3);
    white-space: nowrap;
  }

  /* Display type, sized for a phone. */
  .r-fs56 { font-size: 34px !important; }
  .r-fs40 { font-size: 28px !important; }
  .r-fs32 { font-size: 25px !important; }
  .r-fs30 { font-size: 24px !important; }
  .r-fs27 { font-size: 22px !important; }
  .r-fs24 { font-size: 20px !important; }

  /* Taller hit areas for touch, without changing how anything is laid out. */
  .r-tap { padding-top: 12px !important; padding-bottom: 12px !important; }
}

/* The plan table's closing footnote shares the row grid but holds one
   spanning note, not six cells, so it collapses on its own terms. */
@media (max-width: 640px) {
  .r-rownote {
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 10px !important;
    padding: 14px 16px !important;
  }
  .r-rownote > :first-child { display: none !important; }
  .r-rownote > * { grid-column: 1 / -1 !important; }
}

/* On a narrow phone the brand's place suffix is what stops the wordmark and
   the account chip sharing a row, which would otherwise cost a third sticky
   row. "Ambio" carries the brand; the borough is named throughout the page
   and in the tab title. */
@media (max-width: 420px) {
  .r-topbar > :first-child > span > span { display: none; }
}
