/* Load after the original styles. Keep wide content inside its own scroller. */
.main-layout {
  grid-template-columns: 300px minmax(0, 1fr);
}

.main-layout > *,
.essay-article,
.essay-article > *,
.prose,
.prose > *,
.cards-grid > *,
.thought-flow-ribbon > * {
  min-width: 0;
}

.essay-stream,
.table-container,
.spec-block,
.figure-frame {
  max-width: 100%;
}

.cards-grid {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
}

.thought-flow-ribbon {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr));
}

.masthead h1,
.masthead-deck,
.kicker,
.essay-title,
.essay-standfirst,
.prose,
.toc-link,
.flow-step,
.nav-btn {
  overflow-wrap: anywhere;
}

.essay-meta {
  flex-wrap: wrap;
}

.table-container,
.spec-block {
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

.essay-article {
  scroll-margin-top: 1.25rem;
}

/* Fit the zoomed image inside the modal, including a short landscape viewport. */
#lightbox {
  padding: 5rem 1.25rem 1.25rem;
}

#lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

#lightbox-close {
  color: var(--text-heading);
  min-height: 44px;
}

.toc-toggle,
.toc-backdrop,
.toc-panel-header {
  display: none;
}

.toc-toggle:focus-visible,
.toc-close:focus-visible {
  outline: 3px solid var(--accent-gold);
  outline-offset: 3px;
}

@media (min-width: 1025px) {
  #chapter-1 .responsive-table th:first-child,
  #chapter-1 .responsive-table td:first-child {
    width: 8.5rem;
    min-width: 8.5rem;
    padding-left: 0.85rem;
    padding-right: 0.85rem;
    overflow-wrap: normal;
    word-break: normal;
  }

  #chapter-1 .responsive-table th:first-child,
  #chapter-1 .responsive-table td:first-child strong {
    white-space: nowrap;
  }

  #chapter-1 .responsive-table td:first-child strong {
    font-size: 1rem;
  }
}

@media (max-width: 1024px) {
  html.toc-open,
  body.toc-open {
    overflow: hidden;
  }

  .main-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 2rem;
  }

  .toc-toggle {
    position: fixed;
    left: calc(0.75rem + env(safe-area-inset-left, 0px));
    bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
    z-index: 9000;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 48px;
    padding: 0.75rem 1rem;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 999px;
    background: var(--accent-cyan);
    color: #ffffff;
    box-shadow: 0 6px 24px rgba(8, 126, 155, 0.3);
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
  }

  .toc-toggle svg {
    flex-shrink: 0;
  }

  .toc-open .toc-toggle {
    visibility: hidden;
  }

  .toc-backdrop {
    position: fixed;
    inset: 0;
    z-index: 10010;
    display: block;
    background: rgba(16, 42, 67, 0.45);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.24s ease, visibility 0s 0.24s;
  }

  .toc-open .toc-backdrop {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transition-delay: 0s;
  }

  .toc-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 10011;
    width: min(360px, calc(100% - 2rem));
    height: 100vh;
    height: 100dvh;
    max-height: none;
    padding: 1rem;
    padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
    border: none;
    border-radius: 0 16px 16px 0;
    box-shadow: 12px 0 40px rgba(16, 42, 67, 0.18);
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    transform: translateX(calc(-100% - 1rem));
    visibility: hidden;
    pointer-events: none;
    transition: transform 0.24s ease, visibility 0s 0.24s;
  }

  .toc-open .toc-sidebar {
    transform: translateX(0);
    visibility: visible;
    pointer-events: auto;
    transition-delay: 0s;
  }

  .toc-panel-header {
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin: -1rem -1rem 1rem;
    padding: 0.75rem 1rem;
    padding-top: calc(0.75rem + env(safe-area-inset-top, 0px));
    background: var(--bg-paper);
    border-bottom: 1px solid var(--border-subtle);
  }

  .toc-panel-header h2 {
    color: var(--text-heading);
    font-size: 1rem;
    line-height: 1.4;
  }

  .toc-close {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border: 1px solid var(--border-subtle);
    border-radius: 10px;
    background: var(--bg-surface);
    color: var(--text-heading);
    font-size: 1rem;
    cursor: pointer;
  }

  .toc-list {
    display: flex;
    flex-direction: column;
  }

  .mode-btn {
    min-height: 44px;
    line-height: 1.5;
  }

  /* Each table row becomes a card, with a label above each original value. */
  .table-container {
    overflow: visible;
    border: 0;
    border-radius: 0;
  }

  .table-container .responsive-table,
  #chapter-2 .chapter2-layers {
    position: relative;
    display: block;
    width: 100%;
    min-width: 0;
    background: transparent;
    font-size: 1rem;
  }

  /* Keep column headings available to screen readers. */
  .responsive-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  .responsive-table tbody {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
  }

  .responsive-table tbody tr {
    display: block;
    min-width: 0;
    border: 1px solid var(--border-subtle);
    border-radius: 12px;
    background: var(--bg-paper);
    box-shadow: 0 4px 14px rgba(29, 56, 79, 0.04);
    overflow: hidden;
  }

  .responsive-table tbody td,
  #chapter-2 .chapter2-layers td:first-child {
    display: block;
    width: 100%;
    min-width: 0;
    padding: 1rem;
    border-bottom: 0;
    color: var(--text-main);
    line-height: 1.65;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .responsive-table tbody td + td {
    border-top: 1px solid var(--border-subtle);
  }

  .responsive-table td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 0.4rem;
    color: var(--text-muted);
    font-size: 0.7rem;
    font-weight: 700;
    line-height: 1.5;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  #chapter-2 .chapter2-layers tbody tr:nth-child(even) td {
    background: var(--bg-paper);
  }

  .responsive-table tbody td:first-child,
  #chapter-2 .chapter2-layers tbody tr td:first-child {
    background: var(--bg-highlight);
    color: var(--text-heading);
    font-size: 1.1rem;
    font-weight: 700;
    line-height: 1.5;
  }

  .responsive-table td:first-child::before {
    color: var(--accent-cyan);
  }

  .responsive-table td:first-child small {
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 400;
  }
}

@media (prefers-reduced-motion: reduce) {
  .toc-sidebar,
  .toc-backdrop {
    transition: none;
  }
}

@media (max-width: 640px) {
  body {
    font-size: 16px;
    line-height: 1.75;
  }

  .masthead {
    padding: 2.5rem 1rem 2rem;
  }

  .kicker {
    max-width: 100%;
    padding: 0.5rem 0.75rem;
    margin-bottom: 1rem;
    font-size: 0.65rem;
    line-height: 1.65;
    letter-spacing: 0.07em;
    border-radius: 14px;
  }

  .masthead h1 {
    font-size: clamp(1.8rem, 7vw, 2.2rem);
    line-height: 1.22;
    margin-bottom: 1rem;
  }

  .masthead-deck {
    font-size: 1rem;
    line-height: 1.75;
    margin-bottom: 1.5rem;
  }

  .thought-flow-ribbon {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.6rem;
    padding-top: 1.25rem;
  }

  .flow-step {
    padding: 0.75rem;
  }

  .flow-step span {
    font-size: 0.65rem;
  }

  .flow-step strong {
    font-size: 0.8rem;
    line-height: 1.5;
  }

  .main-layout {
    gap: 1.5rem;
    padding: 1.5rem 0.75rem 3rem;
  }

  .toc-sidebar {
    padding: 1rem;
    padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  }

  .toc-list {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.25rem;
  }

  .toc-link {
    padding: 0.6rem 0.7rem;
  }

  .mode-btn,
  .nav-btn {
    min-height: 44px;
  }

  .mode-btn {
    line-height: 1.5;
  }

  .essay-stream {
    gap: 2rem;
  }

  .essay-article {
    padding: 1.5rem 1rem;
    border-radius: 14px;
  }

  .essay-article::before {
    border-radius: 14px 14px 0 0;
  }

  .essay-meta {
    align-items: flex-start;
    gap: 0.5rem;
  }

  .essay-badge {
    font-size: 0.7rem;
  }

  .essay-reading-time {
    flex-basis: 100%;
    font-size: 0.78rem;
  }

  .essay-title {
    font-size: clamp(1.55rem, 6vw, 1.85rem);
    line-height: 1.3;
  }

  .essay-standfirst {
    font-size: 1.1rem;
    padding-bottom: 1.25rem;
    margin-bottom: 1.5rem;
  }

  .prose h3 {
    font-size: 1.35rem;
    margin-top: 2rem;
  }

  .prose h4 {
    font-size: 1rem;
  }

  .prose ul,
  .prose ol {
    margin-left: 1.2rem;
  }

  .pullquote,
  .reflection-box,
  .wbs-visual {
    padding: 1rem;
    margin: 1.5rem 0;
  }

  .pullquote p {
    font-size: 1.1rem;
  }

  .figure-frame {
    padding: 0.6rem;
    margin: 1.5rem 0;
  }

  .figure-caption {
    padding: 0;
    font-size: 0.8rem;
    gap: 0.5rem;
  }

  th,
  td {
    padding: 0.75rem;
  }

  #chapter-2 .domain-appendix > summary {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 1rem;
  }

  #chapter-2 .domain-appendix > summary::after {
    float: none;
    flex-shrink: 0;
  }

  #chapter-2 .domain-appendix-body {
    padding: 0 0.75rem 0.75rem;
  }

  .cards-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.75rem;
  }

  .insight-card {
    padding: 1rem;
  }

  .wbs-legend {
    gap: 0.5rem 0.75rem;
  }

  .wbs-head {
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
  }

  .wbs-bar div {
    min-width: 0;
    font-size: 0.6rem;
  }

  .spec-block {
    padding: 0.9rem;
    font-size: 0.75rem;
  }

  .essay-footer {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.65rem;
    margin-top: 2rem;
  }

  .nav-btn {
    width: 100%;
    padding: 0.75rem;
    text-align: center;
  }

  #lightbox {
    padding: 4.5rem 0.75rem 1rem;
    padding-top: calc(4.5rem + env(safe-area-inset-top, 0px));
    padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  }

  #lightbox-close {
    top: calc(0.75rem + env(safe-area-inset-top, 0px));
    right: calc(0.75rem + env(safe-area-inset-right, 0px));
  }
}
