.mobile-detail-heading {
  display: none;
}

.detail-toolbar {
  display: contents;
}

@media (max-width: 700px), (max-height: 500px) and (pointer: coarse) {
  :root {
    --mobile-gutter: max(clamp(16px, 5vw, 28px), env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px));
    --mobile-top: env(safe-area-inset-top, 0px);
    --mobile-bottom: env(safe-area-inset-bottom, 0px);
  }

  body:has(.grid) {
    --mobile-home-gutter: max(9vw, calc(var(--mobile-gutter) + 10px));
    --home-w: min(
      calc(100vw - 2 * var(--mobile-home-gutter)),
      clamp(160px, calc(100svh - var(--mobile-top) - var(--mobile-bottom) - 120px), 560px)
    );
    --home-h: var(--home-w);
  }

  body:has(.grid) header {
    padding: calc(8px + var(--mobile-top)) var(--mobile-gutter) 8px;
  }

  body:has(.grid) header > a {
    display: inline-flex;
    align-items: center;
    gap: .3em;
    min-height: 44px;
  }

  html body .grid > a.cell {
    padding: 8px !important;
  }

  html body .grid > a.cell > h2 {
    font-size: min(clamp(17px, 5.7vw, 28px), calc(var(--home-w) / 10)) !important;
  }

  html body .grid > a.cell > p {
    font-size: clamp(9px, 2.65vw, 12px) !important;
    white-space: normal !important;
  }

  body footer,
  body:has(.grid) footer {
    padding: 12px var(--mobile-gutter) calc(12px + var(--mobile-bottom));
  }

  body:has(.split) header {
    display: none;
  }

  .split .back {
    position: static;
    justify-self: start;
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    margin: 0;
    font-size: 14px;
  }

  .split:has(.urban-carousel),
  .split.personal-page {
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
    gap: 14px;
    height: auto;
    min-height: 100svh;
    padding: calc(8px + var(--mobile-top)) var(--mobile-gutter) calc(54px + var(--mobile-bottom));
  }

  .split-title,
  .mobile-detail-heading {
    text-align: center;
  }

  .split-title h1 {
    font-size: clamp(27px, 7.7vw, 40px);
  }

  .split-title p {
    font-size: clamp(14px, 4.3vw, 21px);
    margin-top: 2px;
  }

  .urban-carousel {
    --mobile-dot-size: 28px;
    --mobile-dot-scale: calc(1 / 3);
    --mobile-carousel-side: max(
      var(--mobile-gutter),
      calc(max(env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px), 6px) + var(--mobile-dot-size) + 4px)
    );
    --mobile-card-size: min(
      calc(100vw - 2 * var(--mobile-carousel-side)),
      clamp(180px, calc((100svh - var(--mobile-top) - var(--mobile-bottom)) * .58 - 120px), 450px)
    );
    --mobile-stage-height: calc(var(--mobile-card-size) * 1.75);
    --card-w: var(--mobile-card-size);
    --card-h: var(--mobile-card-size);
    --near-card-offset: .57;
    --far-card-offset: .68;
    display: grid;
    justify-items: center;
    gap: 6px;
    height: auto;
  }

  .urban-carousel .card-track {
    width: var(--mobile-card-size);
    height: var(--mobile-stage-height);
    touch-action: pinch-zoom;
  }

  .urban-carousel .work-card {
    transition-duration: 600ms;
  }

  .urban-carousel .card-caption {
    padding: 40px 16px 16px;
    gap: 4px;
  }
  .urban-carousel .card-caption strong { font-size: clamp(20px, 5.6vw, 24px); }
  .urban-carousel .card-caption small { font-size: 11px; }
  .urban-carousel .carousel-controls {
    top: calc(var(--mobile-stage-height) / 2);
    /* Average the card's right edge and the page's right edge. */
    left: calc((50% + var(--mobile-card-size) / 2 + 100% + var(--mobile-gutter)) / 2);
    right: auto;
    gap: 0;
    transform: translate(-50%, -50%) scaleY(var(--mobile-dot-scale));
  }

  .urban-carousel .carousel-controls button > span {
    transform: scaleX(var(--mobile-dot-scale));
  }

  .urban-carousel .carousel-controls button {
    width: var(--mobile-dot-size);
    height: var(--mobile-dot-size);
    padding: 0;
  }

  .urban-carousel .carousel-hint {
    position: static;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
    color: #52584c;
  }

  .urban-carousel:not(:has([data-slide]:nth-child(2))) .carousel-hint { display: none; }

  .project-dialog.portfolio-view {
    inset: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    padding: var(--mobile-top) var(--mobile-gutter) calc(24px + var(--mobile-bottom));
    background: #f5f4ef;
    scroll-snap-type: none;
  }

  .portfolio-view .detail-toolbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    justify-content: flex-end;
    margin-top: calc(-1 * var(--mobile-top));
    padding: calc(8px + var(--mobile-top)) 0 8px;
    background: #f5f4ef;
  }

  .project-dialog.portfolio-view .close-card {
    position: relative !important;
    inset: auto !important;
    width: 44px !important;
    height: 44px !important;
    transform: none;
  }

  .mobile-detail-heading {
    display: grid;
    gap: 5px;
    width: min(100%, var(--detail-card-limit));
    margin: 0 auto 18px;
    overflow-wrap: anywhere;
  }

  .mobile-detail-heading h2 {
    font-size: clamp(24px, 6.5vw, 36px);
    font-weight: inherit;
    line-height: 1.35;
  }

  .mobile-detail-heading p { color: #5e655a; font-size: 13px; line-height: 1.5; }
  .project-dialog.portfolio-view .project-cover,
  .project-dialog.portfolio-view .project-image { margin-bottom: 14px; }

  .portfolio-view .original-image {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    top: 6px;
    right: 6px;
    font-size: 12px;
  }

  .compare-label { top: 8px; padding: 4px 6px; }
  .before-label { left: 8px; }
  .after-label { right: 8px; }

  .split.personal-page {
    min-height: calc(100svh - 40px);
    padding-bottom: 18px;
    overflow-x: clip;
  }

  .personal-page .personal-contact { position: static; margin-top: 14px; }

  .personal-bio-frame {
    position: static;
    margin-inline: 0;
    padding: 0;
    min-width: 0;
    overflow: visible;
  }

  .personal-bio {
    width: 100%;
    aspect-ratio: auto;
    overflow: visible;
    padding: clamp(18px, 6vw, 32px);
    border-radius: 22px;
  }

  .personal-bio-copy { line-height: 1.8; }

  body:has(.personal-page) footer {
    position: static;
  }
}
