/* ============================================================
   Chapters: a subject page read one chapter at a time. `chapters.js`
   builds the chrome; this places it. A phone gets a strip of chips under
   the bar and a spine fixed to the bottom; a desktop gets a sidebar with
   the chapter list and a progress block, and the breadcrumb in the bar.
   The subject's accent is the rung, the open chip and the bar fill, and
   nothing else.
   ============================================================ */

[data-accent="crimson"] { --accent: var(--crimson); }
[data-accent="forest"]  { --accent: var(--forest); }
[data-accent="slate"]   { --accent: var(--slate); }
[data-accent="ochre"]   { --accent: var(--ochre); }
[data-accent="ink"]     { --accent: var(--ink); }

/* A block in a chapter that is not the open one. Its own attribute, with
   weight behind it, because a block that sets its own display beats the
   browser's `hidden`. */
[data-chap-off] { display: none !important; }
.chap { display: grid; grid-template-columns: minmax(0, 1fr); }
.chap__main { min-width: 0; }
.chap__side { display: none; }
.chap__strip, .topbar__chips { display: none; }
.chap__spine, .chap__sheet, .chap__scrim { display: none; }
html[data-chap-sheet] { overflow: hidden; }

/* ---------- the strip and the spine, on a phone ---------- */

@media (max-width: 899px) {
  /* The chips, in the bar beside the subject. A sideways scroller, and
     named as one: eight chapter chips do not fit on a phone and the design
     puts them in the bar. The open chip is scrolled into view, so the last
     item is never the one hidden. */
  .topbar__chips { flex: 1 1 auto; min-width: 0; display: flex; }
  .chap__strip {
    display: block; flex: 1 1 auto; min-width: 0; position: relative;
    overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent);
    mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent);
  }
  .chap__strip::-webkit-scrollbar { display: none; }
  .chap__strip-in { display: flex; gap: 6px; padding: 0 24px 0 0; width: max-content; }
  .chap__chip {
    display: inline-flex; align-items: center; min-height: 44px;
    padding: 8px 11px; border-radius: 999px; white-space: nowrap;
    border: 1px solid var(--rule-strong); background: var(--paper-card);
    font-family: var(--font-ui); font-weight: 600; font-size: 11.5px; line-height: 1;
    color: var(--ink); text-decoration: none;
  }
  .chap__chip[aria-current="true"] {
    background: var(--accent, var(--ink)); border-color: var(--accent, var(--ink)); color: #FFFFFF;
  }
  html[data-chap] body { padding-bottom: 76px; }
  .chap__spine {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
    padding: 2px 8px 10px;
    background: var(--paper-card); border-top: 1px solid var(--rule);
  }
  .chap__spine-row {
    display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 6px;
    min-height: 44px;
    font-family: var(--font-ui); font-weight: 600; font-size: 12px; line-height: 1.3;
  }
  .chap__spine-prev, .chap__spine-next {
    display: inline-flex; align-items: center; min-height: 44px; padding: 0 10px;
    border-radius: 999px; color: var(--ink); text-decoration: none; white-space: nowrap;
  }
  .chap__spine-prev:active, .chap__spine-next:active { background: var(--rule-soft); }
  .chap__spine-here {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 44px; min-width: 0; padding: 0 8px; border: 0; border-radius: 999px;
    background: none; color: var(--ink-faint); font: inherit; cursor: pointer;
  }
  .chap__spine-where { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .chap__spine-caret { flex: 0 0 auto; font-size: 9px; color: var(--ink-faint); }
  .chap__spine-here[aria-expanded="true"] .chap__spine-caret { transform: rotate(180deg); }
  .chap__spine .chap__bar { margin: 0 12px; }

  /* The list of every chapter, as a sheet up from the spine. */
  .chap__sheet[hidden], .chap__scrim[hidden] { display: none; }
  .chap__scrim {
    display: block; position: fixed; inset: 0; z-index: 46;
    background: rgba(26, 31, 29, 0.35);
  }
  .chap__sheet {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 47;
    max-height: 78vh; overflow: auto;
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    border-radius: 16px 16px 0 0;
    background: var(--paper-card); box-shadow: 0 -8px 24px rgba(26, 31, 29, 0.12);
  }
  .chap__sheet-head {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    min-height: 44px; margin-bottom: 6px;
  }
  .chap__sheet-title {
    font-family: var(--font-ui); font-weight: 800; font-size: 15px; letter-spacing: -0.02em;
    color: var(--ink);
  }
  .chap__sheet-close {
    min-height: 44px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--rule-strong);
    background: var(--paper-card); color: var(--ink); cursor: pointer;
    font-family: var(--font-ui); font-weight: 600; font-size: 12px;
  }
  .chap__sheet-list { display: flex; flex-direction: column; gap: 4px; }
  .chap__sheet-item {
    display: flex; align-items: center; min-height: 48px; padding: 0 14px; border-radius: 8px;
    font-family: var(--font-ui); font-weight: 600; font-size: 15px; line-height: 1.3;
    color: var(--ink); text-decoration: none;
  }
  .chap__sheet-item[aria-current="true"] { background: var(--accent, var(--ink)); color: #FFFFFF; }
  .chap__sheet-foot { margin-top: 10px; border-top: 1px solid var(--rule); }
  .chap__sheet-out {
    display: flex; align-items: center; width: 100%; min-height: 48px; padding: 0 14px;
    border: 0; background: none; cursor: pointer; text-align: left;
    font-family: var(--font-ui); font-weight: 600; font-size: 14px;
    color: var(--forest); text-decoration: none;
  }
  .chap__level--sheet { margin-top: 10px; padding: 10px 14px 4px; border-top: 1px solid var(--rule); }
}

/* ---------- the sidebar, on a desktop ---------- */

@media (min-width: 900px) {
  .chap {
    grid-template-columns: 280px minmax(0, 1fr);
    max-width: 1360px; margin: 0 auto;
  }
  .chap__side {
    display: block; position: sticky; top: var(--stuck, 76px);
    align-self: start; max-height: calc(100vh - var(--stuck, 76px));
    overflow: auto;
    padding: 28px 24px;
    border-right: 1px solid var(--rule); background: var(--paper-2);
  }
  .chap__main { padding: 0 0 var(--s5); }
  /* The words stop at the measure and the pictures at the canvas, as on
     every page. The column just has less canvas to give. */
}

.chap__subject { display: flex; align-items: center; gap: 10px; }
.chap__rung { width: 4px; height: 24px; border-radius: 2px; background: var(--accent, var(--ink)); }
.chap__name {
  font-family: var(--font-ui); font-weight: 800; font-size: 17px; line-height: 1;
  letter-spacing: -0.02em; color: var(--ink);
}
.chap__count { font-family: var(--font-ui); font-weight: 600; font-size: 11px; color: var(--ink-faint); }
.chap__list { display: flex; flex-direction: column; gap: 4px; margin-top: 16px; }
.chap__item {
  display: block; padding: 11px 12px; border-radius: 6px;
  font-family: var(--font-ui); font-weight: 600; font-size: 13.5px; line-height: 1.3;
  color: var(--ink-soft); text-decoration: none;
}
.chap__item:hover { background: var(--hover-cream); color: var(--ink); }
.chap__item[aria-current="true"] { background: var(--accent, var(--ink)); color: #FFFFFF; }
.chap__prog { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--rule); }
.chap__prog-row {
  display: flex; justify-content: space-between;
  font-family: var(--font-ui); font-weight: 600; font-size: 11px; line-height: 1;
  color: var(--ink-faint);
}
.chap__prog .chap__bar { margin-top: 8px; }
.chap__bar { height: 3px; border-radius: 2px; overflow: hidden; background: var(--rule); }
.chap__bar i {
  display: block; height: 100%; width: 0; background: var(--accent, var(--ink));
  transition: width .3s ease;
}

/* ---------- the next chapter card ---------- */

.chap__next {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  max-width: 1080px; margin: var(--s4) auto 0; padding: 18px 22px; border-radius: 8px;
  background: var(--deep); color: var(--cream-on-deep); text-decoration: none;
  box-sizing: border-box;
}
.chap__next-e {
  display: block;
  font-family: var(--font-hand); font-weight: 500; font-size: 10px; line-height: 1;
  letter-spacing: 0.12em; text-transform: uppercase; color: var(--gold);
}
.chap__next-t {
  display: block; margin-top: 6px;
  font-family: var(--font-ui); font-weight: 700; font-size: 16px; line-height: 1.4;
}
.chap__next-go { font-family: var(--font-ui); font-weight: 600; font-size: 18px; line-height: 1; }
@media (min-width: 900px) {
  .chap__next { margin-left: 24px; margin-right: 24px; max-width: none; }
}
@media (max-width: 899px) {
  .chap__next { margin: var(--s4) 24px 0; max-width: none; }
}
@media (max-width: 720px) {
  .chap__next { margin: var(--s4) 14px 0; padding: 16px 18px; }
}

/* ---------- the chapter heading, as the design draws it ---------- */

html[data-chap] .chapter { border-top: 0; padding-top: 0; margin-top: var(--s5); }
html[data-chap] .chapter__n {
  font-family: var(--font-hand); font-weight: 500; font-size: 10.5px; line-height: 1;
  letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent, var(--ink));
}
html[data-chap] .chapter__t {
  font-family: var(--font-display); font-weight: 800; font-size: 34px; line-height: 1.06;
  letter-spacing: -0.025em; margin-top: 10px;
}
@media (max-width: 640px) {
  html[data-chap] .chapter__t { font-size: 27px; line-height: 1.08; letter-spacing: -0.02em; }
}

/* ---------- the clinic, folded behind a disclosure ---------- */

.feat__open {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 0; border: 0; background: none; cursor: pointer; text-align: left;
  color: var(--ink); min-height: 44px;
}
.feat__open-e {
  flex: 0 0 auto;
  font-family: var(--font-hand); font-weight: 600; font-size: 0.72rem; line-height: 1.3;
  letter-spacing: 0.1em; text-transform: uppercase; color: var(--ochre);
}
.feat__open-t {
  flex: 1 1 auto;
  font-family: var(--font-display); font-weight: 700; font-size: 1.15rem; line-height: 1.25;
  letter-spacing: -0.015em;
}
.feat__open-go { flex: 0 0 auto; font-family: var(--font-ui); font-size: 16px; color: var(--ink-faint); }
.feat--clinic[data-folded="0"] .feat__open-go { transform: rotate(180deg); }
.feat--clinic[data-folded="0"] .feat__open { margin-bottom: var(--s3); }
.feat--clinic[data-folded] { padding: 18px 22px; }

/* ---------- the reading level ----------

   "For now" hides the cards marked for later behind one button a chapter;
   "Everything" shows the lot. Remembered, so it is asked once. */
.chap__level { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--rule); }
.chap__level-l {
  display: block; margin-bottom: 8px;
  font-family: var(--font-hand); font-weight: 500; font-size: 10.5px; line-height: 1;
  letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink-faint);
}
.chap__level-g { display: inline-flex; border: 1px solid var(--rule-strong); border-radius: 999px; padding: 2px; }
.chap__level-b {
  min-height: 32px; padding: 0 12px; border: 0; border-radius: 999px; background: none;
  font-family: var(--font-ui); font-weight: 600; font-size: 12px; color: var(--ink-soft); cursor: pointer;
}
.chap__level-b[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
@media (max-width: 899px) { .chap__level-b { min-height: 40px; } }
[data-rung-off] { display: none !important; }
.chap__more[hidden] { display: none; }
.chap__more {
  display: block; width: auto; max-width: 1080px; margin: var(--s3) auto 0; min-height: 44px;
  box-sizing: border-box;
  padding: 12px 16px; border-radius: 8px; border: 1px dashed var(--rule-strong);
  background: var(--paper-card); color: var(--ink-soft); cursor: pointer;
  font-family: var(--font-ui); font-weight: 600; font-size: 13px;
}
.chap__more:hover { color: var(--ink); border-color: var(--ink); }
@media (min-width: 900px) { .chap__more { margin-left: 24px; margin-right: 24px; max-width: none; } }
@media (max-width: 899px) { .chap__more { margin: var(--s3) 24px 0; max-width: none; } }
@media (max-width: 720px) { .chap__more { margin: var(--s3) 14px 0; } }

/* ---------- every idea a row ----------

   The card's head is the row: mark, title, the sentence that led in, the
   rung. Tap to open the rest. The bridges between cards are put away while
   the rows are on, because the rows carry their sentences. */
html[data-rows] p.bridge { display: none !important; }
.idea[data-row] { padding: 0; }
.idea[data-row] .idea__head {
  cursor: pointer; margin: 0; min-height: 56px; padding: 12px 44px 4px 19px;
  display: flex; align-items: center; gap: 12px;
}
.idea[data-row] .idea__head:focus-visible { outline: 2px solid var(--ink); outline-offset: -2px; }
.idea[data-row] .idea__head h4 { margin: 0; }
.idea[data-row] .idea__caret {
  position: absolute; right: 17px; top: 18px;
  font-family: var(--font-ui); font-size: 14px; color: var(--ink-faint);
  transition: transform .18s ease;
}
.idea[data-row][data-row-open="1"] .idea__caret { transform: rotate(180deg); }
.idea[data-row] .idea__sub {
  margin: 0; padding: 0 44px 14px 19px;
  font-family: var(--font-body); font-weight: 300; font-size: 14.5px; line-height: 1.45;
  color: var(--ink-soft);
}
.idea[data-row] .idea__sub:empty { display: none; padding: 0; }
.idea[data-row] .rung { top: auto; bottom: 14px; right: 17px; }
.idea[data-row] .idea__body { padding: 0 19px 15px; border-top: 1px solid var(--rule); }
.idea[data-row] .idea__body > p:first-child { margin-top: 12px; }
.idea[data-row][data-row-open="1"] { border-color: var(--rule-strong); }
@media (max-width: 720px) {
  .idea[data-row] .idea__head { padding: 12px 40px 4px 15px; }
  .idea[data-row] .idea__sub { padding: 0 40px 12px 15px; }
  .idea[data-row] .idea__body { padding: 0 15px 12px; }
  .idea[data-row] .idea__caret { right: 15px; }
  .idea[data-row] .rung { right: 15px; }
}
