.app {
  display: grid;
  /* An implicit `auto` column would be sized by max-content and let the
     chapter strip widen the page. */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
}

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-base);
  min-height: var(--topbar-height);
  padding: var(--space-tight) var(--space-wide) var(--space-tight) var(--space-base);
  border-bottom: 1px solid var(--hairline);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-snug);
  min-width: 0;
}

.brand__mark {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  flex: 0 0 auto;
  font-family: var(--font-display);
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--color-paper);
  background: linear-gradient(150deg, var(--color-gold), var(--color-gold-deep));
  border-radius: 50% 50% 50% 4px;
  box-shadow: var(--shadow-lift);
}

.brand__text {
  min-width: 0;
}

.brand__title {
  font-family: var(--font-display);
  font-size: clamp(1.05rem, 0.9rem + 0.6vw, 1.5rem);
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-tight);
}

.field {
  display: flex;
  align-items: center;
  gap: var(--space-tight);
  padding: 0.3rem 0.35rem 0.3rem 0.7rem;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
  background: var(--surface-rail);
  transition: border-color var(--duration-fast) var(--ease-soft);
}

.field:hover {
  border-color: var(--color-paper-edge);
}

.field__label {
  font-size: var(--text-micro);
  font-weight: 600;
  letter-spacing: var(--tracking-eyebrow);
  text-transform: uppercase;
  color: var(--color-ink-faint);
}

.field select {
  font: inherit;
  font-size: var(--text-small);
  font-weight: 500;
  color: inherit;
  padding: 0.15rem 0.4rem;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  cursor: pointer;
}

.ghost-button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-hair);
  padding: 0.55rem 0.7rem;
  color: var(--color-ink-soft);
  border-radius: var(--radius-pill);
  transition:
    color var(--duration-fast) var(--ease-soft),
    background-color var(--duration-fast) var(--ease-soft);
}

.ghost-button:hover {
  color: var(--color-ink);
  background-color: oklch(26% 0.03 60 / 0.06);
}

.ghost-button[aria-pressed="true"] {
  color: var(--color-gold-deep);
  background-color: oklch(78% 0.145 78 / 0.16);
}

/* ---------- Body grid ---------- */

.workspace {
  display: grid;
  grid-template-columns: var(--rail-width) minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
}

/* ---------- Chapter rail ---------- */

.rail {
  display: flex;
  flex-direction: column;
  /* Grid and flex children default to min-width:auto — without this the
     horizontal chapter strip stretches the whole page on small screens. */
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--hairline);
  background: linear-gradient(
    180deg,
    var(--surface-rail),
    var(--color-paper-deep)
  );
}

.rail__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-tight);
  padding: var(--space-base) var(--space-base) var(--space-snug);
}

.rail__count {
  font-family: var(--font-mono);
  font-size: var(--text-micro);
  color: var(--color-ink-faint);
}

.rail__list {
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 0 var(--space-snug) var(--space-loose);
  scrollbar-width: thin;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.chapter {
  position: relative;
  display: grid;
  grid-template-columns: 2.4rem minmax(0, 1fr);
  gap: var(--space-snug);
  width: 100%;
  padding: var(--space-snug) var(--space-snug) var(--space-snug) var(--space-tight);
  text-align: left;
  border-radius: var(--radius-md);
  transition:
    background-color var(--duration-fast) var(--ease-soft),
    transform var(--duration-normal) var(--ease-out-expo);
}

.chapter + .chapter {
  margin-top: 2px;
}

.chapter::before {
  content: "";
  position: absolute;
  inset: 0.35rem auto 0.35rem -0.25rem;
  width: 2px;
  border-radius: var(--radius-pill);
  background: var(--color-gold);
  opacity: 0;
  transform: scaleY(0.3);
  transform-origin: center;
  transition:
    opacity var(--duration-normal) var(--ease-out-expo),
    transform var(--duration-normal) var(--ease-out-expo);
}

.chapter:hover {
  background-color: oklch(26% 0.03 60 / 0.05);
}

.chapter:hover .chapter__title {
  color: var(--color-ink);
}

.chapter[aria-current="true"] {
  background-color: oklch(78% 0.145 78 / 0.14);
}

.chapter[aria-current="true"]::before {
  opacity: 1;
  transform: scaleY(1);
}

.chapter__number {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  padding-top: 0.1rem;
  color: var(--color-ink-faint);
  font-variant-numeric: tabular-nums;
  transition: color var(--duration-fast) var(--ease-soft);
}

.chapter[aria-current="true"] .chapter__number {
  color: var(--color-gold-deep);
}

.chapter__title {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  font-weight: 600;
  line-height: 1.25;
  color: var(--color-ink-soft);
  transition: color var(--duration-fast) var(--ease-soft);
}

.chapter[aria-current="true"] .chapter__title {
  color: var(--color-ink);
}

.chapter__meta {
  display: flex;
  align-items: center;
  gap: var(--space-tight);
  margin-top: 0.2rem;
  font-size: var(--text-micro);
  color: var(--color-ink-faint);
  font-variant-numeric: tabular-nums;
}

.chapter__dot {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.6;
}

.rail__foot {
  padding: var(--space-snug) var(--space-base) var(--space-base);
  border-top: 1px solid var(--hairline);
  font-size: var(--text-micro);
  line-height: 1.5;
  color: var(--color-ink-faint);
}

/* ---------- Responsive ---------- */

/* Laptops and tablets in landscape: keep the side rail, just narrower. */
@media (max-width: 78rem) {
  :root {
    --rail-width: 15.5rem;
  }

  .chapter {
    grid-template-columns: 1.9rem minmax(0, 1fr);
    padding-block: var(--space-tight);
  }

  .chapter__number {
    font-size: 1.25rem;
  }

  .chapter__title {
    font-size: 0.9375rem;
  }
}

@media (max-width: 60rem) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
  }

  .rail {
    grid-row: 2;
    border-right: 0;
    border-top: 1px solid var(--hairline);
  }

  .rail__head,
  .rail__foot {
    display: none;
  }

  .rail__list {
    display: flex;
    gap: var(--space-tight);
    overflow-x: auto;
    overflow-y: hidden;
    padding: var(--space-snug);
    scroll-snap-type: x mandatory;
    /* Horizontal strip: allow the swipe, block the browser's back gesture. */
    touch-action: pan-x;
    overscroll-behavior-x: contain;
  }

  .chapter {
    grid-template-columns: auto;
    gap: 0;
    width: auto;
    min-width: 9.5rem;
    scroll-snap-align: center;
  }

  .chapter::before {
    inset: auto 0.5rem 0.25rem 0.5rem;
    width: auto;
    height: 2px;
    transform: scaleX(0.3);
  }

  .chapter[aria-current="true"]::before {
    transform: scaleX(1);
  }

  .chapter__number {
    font-size: 1rem;
  }

  .chapter__title {
    font-size: 0.875rem;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    /* Reserve both lines so the meta row lines up across the strip. */
    min-height: 2.5em;
  }
}

@media (max-width: 34rem) {
  .topbar {
    min-height: 3.5rem;
    padding-inline: var(--space-tight);
  }

  .brand__eyebrow,
  .field__label {
    display: none;
  }

  .brand__mark {
    width: 2rem;
    height: 2rem;
    font-size: 0.75rem;
  }

  .field {
    padding-left: 0.35rem;
  }

  .field select {
    max-width: 7.5rem;
  }

  .rail__list {
    padding: var(--space-tight);
  }

  .chapter {
    min-width: 8rem;
  }
}

/* Small phones (320–400px): the strip keeps titles, drops the page meta. */
@media (max-width: 25rem) {
  .brand__title {
    font-size: 0.9375rem;
  }

  .field select {
    max-width: 5.5rem;
    font-size: var(--text-micro);
  }

  .chapter {
    min-width: 7rem;
  }

  .chapter__meta {
    display: none;
  }
}

/* Phone in landscape: vertical space is the scarce resource. */
@media (max-height: 32rem) and (orientation: landscape) {
  .topbar {
    min-height: 2.75rem;
    padding-block: 0.2rem;
  }

  .brand__eyebrow {
    display: none;
  }

  .brand__mark {
    width: 1.85rem;
    height: 1.85rem;
    font-size: 0.7rem;
  }

  .rail__list {
    padding-block: 0.3rem;
  }

  /* One compact line per chapter: number and title side by side. */
  .chapter {
    grid-auto-flow: column;
    align-items: baseline;
    gap: 0.4rem;
    min-width: 8.5rem;
    padding: 0.3rem var(--space-tight);
  }

  .chapter__number {
    font-size: 0.9rem;
    padding-top: 0;
  }

  .chapter__title {
    font-size: 0.8125rem;
    -webkit-line-clamp: 1;
  }

  .chapter__meta {
    display: none;
  }
}
