/* Keep the story, map and controls in separate tracks, including browser chrome. */
@media (max-width: 700px), (pointer: coarse), (max-width: 900px) and (max-height: 560px) {
  #hud {
    position: fixed;
    inset: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 92px;
    grid-template-rows: auto minmax(0, 1fr) auto auto auto 112px;
    gap: 6px 10px;
    padding: calc(12px + var(--safe-top)) max(12px, env(safe-area-inset-right, 0px)) calc(12px + var(--safe-bottom)) max(12px, env(safe-area-inset-left, 0px));
    pointer-events: none;
  }

  #hud .topbar {
    position: static;
    grid-area: 1 / 1 / 2 / -1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px 12px;
    align-items: start;
    min-width: 0;
  }

  .game-wordmark { min-width: 0; font-size: 1.25rem; }
  .online-chip { max-width: 100%; min-height: 32px; margin-top: 6px; padding: 5px 8px; font-size: .75rem; }
  #online-count { overflow-wrap: anywhere; }
  .top-actions { grid-area: 1 / 2; gap: 8px; }
  .top-actions .round-button { width: 44px; height: 44px; min-width: 44px; }
  .kindness-counter { min-height: 44px; }
  #hud .place-name {
    grid-area: 2 / 1 / 3 / -1;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 3px 10px;
    max-width: none;
    min-width: 0;
    padding: 0;
    text-align: left;
  }
  .place-name span { font-size: .84rem; }
  .place-name small { margin: 0; font-size: .75rem; }

  #hud .quest-card {
    position: static;
    grid-area: 2 / 1 / 3 / 2;
    align-self: start;
    width: 100%;
    max-width: 400px;
    min-width: 0;
    min-height: 0;
    max-height: min(100%, 230px);
    padding: 9px 11px;
    overflow: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    pointer-events: auto;
    scrollbar-width: thin;
  }
  .quest-card .eyebrow { font-size: .6875rem; letter-spacing: .1em; }
  .quest-card h2 { margin: 5px 0; font-size: 1.1rem; line-height: 1.25; overflow-wrap: anywhere; }
  .quest-card p { font-size: .875rem; line-height: 1.4; overflow-wrap: anywhere; }
  .quest-meta { flex-wrap: wrap; gap: 0 8px; margin-top: 4px; font-size: .75rem; }
  .quest-meta > span { white-space: normal; }
  .quest-meta .text-button { min-height: 44px; padding: 7px 0; font-size: .8125rem; text-align: left; }
  .quest-meta kbd { display: none; }

  #hud .minimap-wrap {
    position: static;
    grid-area: 2 / 2 / 3 / 3;
    align-self: start;
    width: 100%;
    height: auto;
    pointer-events: auto;
  }
  .map-foot { padding: 5px 6px; font-size: .625rem; line-height: 1.4; letter-spacing: .04em; }
  .map-foot #saved-indicator { display: none; }

  #hud .bike-badge {
    position: static;
    grid-area: 3 / 1 / 4 / -1;
    justify-self: end;
    max-width: 100%;
    padding: 7px 10px;
    transform: none;
    white-space: normal;
    font-size: .75rem;
    line-height: 1.4;
    text-align: right;
  }
  .bike-badge kbd { display: none; }

  #hud .greeting-controls {
    position: static;
    grid-area: 4 / 1 / 5 / -1;
    justify-self: end;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    transform: none;
    pointer-events: auto;
  }
  .greeting-controls button { min-height: 44px; min-width: 44px; padding: 8px 12px; font-size: .875rem; }
  .greeting-controls kbd { display: none; }
  #hud .greeting-controls:has(> button.hidden:only-child) { display: none; }

  #hud .interact-prompt {
    position: static;
    grid-area: 5 / 1 / 6 / -1;
    justify-self: end;
    min-width: 44px;
    min-height: 44px;
    max-width: 100%;
    padding: 10px 14px;
    transform: none;
    white-space: normal;
    overflow-wrap: anywhere;
    font-size: .875rem;
    line-height: 1.4;
    text-align: left;
    pointer-events: auto;
  }
  .interact-prompt kbd { display: none; }

  #hud .touch-controls {
    position: relative;
    inset: auto;
    grid-area: 6 / 1 / 7 / -1;
    display: block;
    width: 100%;
    height: 112px;
    pointer-events: none;
  }
  .touch-controls > * { pointer-events: auto; }
  .touch-controls #joystick { width: 100px; height: 100px; left: 0; bottom: 0; }
  .touch-controls #joystick-thumb { left: 28px; top: 28px; }
  .touch-actions { right: 0; bottom: 0; gap: 10px; }
  .touch-actions button { width: 56px; height: 56px; }
  .touch-actions small { font-size: .55rem; letter-spacing: .03em; }
  .touch-controls > p { right: 0; bottom: 72px; max-width: calc(100% - 120px); margin: 0; font-size: .75rem; line-height: 1.4; text-align: right; pointer-events: none; }
  .desktop-controls { display: none; }

  #hud .dialogue { max-height: calc(100dvh - 32px - var(--safe-top) - var(--safe-bottom)); overflow: auto; pointer-events: auto; }
  .dialogue-content { min-width: 0; }
  .dialogue-content p { overflow-wrap: anywhere; }
  #dialogue-buttons button { min-height: 44px; font-size: .875rem; }
}

/* Short landscape views keep the story beside, rather than above, the actions. */
@media (orientation: landscape) and (max-height: 560px) and (max-width: 900px), (orientation: landscape) and (max-height: 560px) and (pointer: coarse) {
  #hud {
    grid-template-columns: minmax(180px, 250px) minmax(0, 1fr) 92px;
    grid-template-rows: auto minmax(0, 1fr) auto 104px;
    gap: 8px 12px;
    padding: calc(10px + var(--safe-top)) max(16px, env(safe-area-inset-right, 0px)) calc(10px + var(--safe-bottom)) max(16px, env(safe-area-inset-left, 0px));
  }
  #hud .topbar { grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr) auto; column-gap: 16px; }
  #hud .place-name { grid-area: 1 / 2; flex-direction: column; flex-wrap: nowrap; align-items: center; gap: 3px; text-align: center; }
  .top-actions { grid-area: 1 / 3; }
  #hud .quest-card { grid-area: 2 / 1 / 4 / 2; max-height: calc(100dvh - 184px - var(--safe-top) - var(--safe-bottom)); }
  #hud .quest-card h2 { font-size: 1.0625rem; }
  #hud .quest-meta { min-height: 18px; }
  #hud #show-map { display: none; }
  #hud .minimap-wrap { grid-area: 2 / 3 / 3 / 4; width: 88px; justify-self: end; }
  #hud .bike-badge { grid-area: 3 / 2 / 4 / 3; justify-self: center; max-width: 230px; text-align: center; }
  #hud #flight-hud { grid-area: 3 / 2 / 4 / 3; max-width: 230px; }
  #hud .interact-prompt { grid-area: 3 / 2 / 4 / 4; }
  #hud .greeting-controls { grid-area: 4 / 2 / 5 / 3; justify-self: center; align-self: end; justify-content: center; z-index: 1; }
  #hud .touch-controls { grid-area: 4 / 1 / 5 / -1; height: 104px; }
}

/* A short portrait viewport still needs a one-column story browser. */
@media (max-width: 700px) {
  .map-modal { overflow: auto; }
  .map-header h2 { padding-right: 32px; }
  .map-content { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .big-map-wrap { height: min(36dvh, 260px); max-height: min(36dvh, 260px); aspect-ratio: auto; }
  .story-list { min-width: 0; max-height: min(32dvh, 300px); }
  .story-card .story-buttons { flex-wrap: wrap; }
  .story-card button { min-height: 44px; font-size: .8125rem; }
}
