/* Shared narrow-screen behavior for the existing application and Jin Yi book.
   Theme, artwork and each feature layout stay in their owning stylesheets. */
@media (max-width: 760px) {
  /* Shrink content and wrap controls, instead of scrolling entire pages sideways. */
  .app-frame,
  .phase1-app,
  .workspace,
  :is(.grid, .metrics, .split, .stack, .panel, .card, .row, .page-heading, .dialog-body) > * {
    min-inline-size: 0;
  }

  :is(.workspace, .dialog-body) :is(h1, h2, h3, p, dd, small, strong),
  .member-room__info {
    overflow-wrap: anywhere;
  }

  .page-heading > .actions:empty { display: none; }

  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .key-values { grid-template-columns: minmax(0, 6.25rem) minmax(0, 1fr); }

  .panel-header,
  .card-footer,
  .progress-labels,
  .member-room__top,
  main > footer > div {
    flex-wrap: wrap;
  }

  :is(.actions, .table-actions, .panel-header, .row, .card-footer, .dialog, .post-actions) > .button {
    max-inline-size: 100%;
    min-block-size: 44px;
    white-space: normal;
    overflow-wrap: anywhere;
    text-align: center;
  }

  /* The mobile navigation must keep permitted management links reachable. */
  .topbar {
    padding-inline: max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right));
    gap: 8px;
  }

  .topbar .top-actions { min-inline-size: 0; gap: 4px; }
  .topbar .top-actions > a.button {
    display: inline-flex;
    padding-inline: 6px;
    font-size: .8125rem;
  }

  .topbar .top-actions > .icon-button {
    position: relative;
    flex: 0 0 44px;
  }

  .topbar .top-actions > .icon-button > small {
    position: absolute;
    inset: 1px 0 auto auto;
    min-inline-size: 1.125rem;
    padding: 0 3px;
    border-radius: 1rem;
    background: var(--accent);
    color: var(--accent-ink);
    font-size: .625rem;
    line-height: 1.3;
    text-align: center;
  }

  .topbar .top-actions .user-menu {
    display: block;
    min-inline-size: 0;
    max-inline-size: min(110px, 30vw);
    padding-inline: 8px;
  }

  .mobile-nav {
    padding-inline: max(4px, env(safe-area-inset-left)) max(4px, env(safe-area-inset-right));
  }

  .mobile-nav a {
    flex: 1 1 0;
    min-inline-size: 44px;
    text-align: center;
  }

  .app-frame:has(> .mobile-nav) .workspace {
    padding-block-end: calc(110px + env(safe-area-inset-bottom, 0px));
  }

  /* Preserve the conversation module's full-height viewport on small phones. */
  body.gh-chat-open .app-frame:has(> .mobile-nav) .workspace {
    padding-block-end: calc(75px + env(safe-area-inset-bottom, 0px));
  }
  .gh-chat { min-block-size: 240px; }
  .gh-chat-audio { max-inline-size: 100%; }
  .gh-chat-text-button { min-block-size: 44px; }

  .tabs {
    flex-wrap: wrap;
    align-items: stretch;
    gap: 4px 6px;
  }
  .tabs a {
    display: inline-flex;
    align-items: center;
    min-block-size: 44px;
    padding-inline: 10px;
    white-space: normal;
  }

  /* Dense data stays in its own horizontal region. Ordinary lists still wrap. */
  :is(.table-wrap, .lo-table-wrap) {
    max-inline-size: 100%;
    margin-inline-end: 0;
    overflow: auto;
    overscroll-behavior-inline: contain;
  }

  :is(.table-wrap, .lo-table-wrap) table:has(thead th:nth-child(4)) { min-inline-size: 24rem; }
  :is(.table-wrap, .lo-table-wrap) td { overflow-wrap: anywhere; }
  .table-wrap :is(th, td) { padding-inline-end: 10px; }

  /* One scroll region per dialog keeps form errors and buttons reachable. */
  dialog[open] {
    max-inline-size: calc(100% - 24px);
    max-block-size: calc(100vh - 24px);
    max-block-size: calc(100dvh - 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    overflow: auto;
  }

  dialog[open].dialog > .dialog-body,
  dialog[open].dialog > form > .dialog-body {
    max-block-size: none;
    overflow: visible;
  }

  .dialog :is(header, footer, .dialog-body) { padding-inline: clamp(12px, 4vw, 23px); }
  .dialog footer { flex-wrap: wrap; }
  dialog header h2 { min-inline-size: 0; overflow-wrap: anywhere; }
  dialog header > button { flex: 0 0 auto; }
  dialog :is(button, .button, .lo-link) { min-block-size: 44px; }

  :is(dialog, .workspace) :is(input, textarea, select):not([type="checkbox"], [type="radio"]),
  .gh-chat-input-row textarea {
    min-inline-size: 0;
    max-inline-size: 100%;
    min-block-size: 44px;
    font-size: 1rem;
  }

  .toast {
    bottom: calc(94px + env(safe-area-inset-bottom, 0px));
    overflow-wrap: anywhere;
  }

  #status {
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    overflow-wrap: anywhere;
  }
}

@media (max-width: 380px) {
  .workspace:not(.chat-workspace),
  [data-release="phase1"] .phase1-app .workspace {
    padding-inline: 12px;
  }

  :is(.workspace, .dialog-body) :is(.panel, .card, .metric) { padding: 14px; }
  .key-values { gap: 8px; }
  .row { gap: 10px; }
  .member-room__top { gap: 4px 12px; }
}
