/* Keep both catalogue columns inside the viewport even with long Q text. */
@media (orientation: landscape) {
  #scr-dex .box { height: 100%; min-height: 0; display: flex; flex-direction: column; }
  #scr-dex .split { flex: 1; min-height: 0; grid-template-columns: minmax(0, 1fr) minmax(240px, 36%); }
  #scr-dex .split > div:first-child { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
  #scr-dex .grid { flex: 1; min-height: 0; max-height: none; overflow-y: auto; align-content: start; }
  #dex-detail { min-width: 0; min-height: 0; overflow-y: auto; overflow-wrap: anywhere; }
  #dex-detail > * { flex-shrink: 0; }
}
/* Portrait-only presentation. Desktop and short landscape retain the original UI.
 * Palette/type inherit the navy pixel device identity and the readable CJK stack. */
@media (max-width: 600px) and (orientation: portrait) {
  html { --portrait-gutter: 12px; }
  .scr {
    padding: calc(12px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    overflow: hidden;
  }
  .scr > .box, #scr-rq-evo > .box, .rq-page > .box {
    width: 100% !important; min-width: 0; max-height: 100% !important;
    padding: 16px 13px; overflow: auto; overscroll-behavior: contain;
    scrollbar-width: thin; scrollbar-color: #547fa5 #0b2153;
    -webkit-overflow-scrolling: touch; scroll-padding-block: 64px 20px;
  }
  .scr .box > .row.between:first-child {
    position: sticky; top: -16px; z-index: 4; margin: -16px -13px 12px;
    padding: 12px 13px 10px; background: #102c5d; border-bottom: 1px solid #467aa6;
    gap: 8px; flex-wrap: nowrap;
  }
  .box h2 { font-size: 22px; line-height: 1.3; }
  .scr .btn { min-height: 44px; padding: 8px 12px; white-space: normal; }
  .scr .row { flex-wrap: wrap; }
  .scr .sub { font-size: 14px; line-height: 1.6; }
  .scr .grid { max-height: none; overflow: visible; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .scr .cell { min-width: 0; }
  .scr .cell b { white-space: normal; overflow-wrap: anywhere; }
  .scr .cell canvas { max-width: 100%; }
  #scr-starters { padding-top: calc(66px + env(safe-area-inset-top)); }
  #scr-starters .box > .row:last-child {
    position: sticky; bottom: -16px; margin: 12px -13px -16px; padding: 12px 13px;
    background: #102c5d; border-top: 1px solid #467aa6; z-index: 3;
  }
  #starter-info { flex-basis: 60%; font-size: 13px; }
  #starter-ok { flex: 1; }
  #scr-menu {
    display: flex; flex-direction: column; align-items: stretch; justify-content: flex-start;
    overflow-y: auto; padding: calc(82px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  }
  #scr-menu .menu-left { width: 100%; flex: 0 0 auto; padding: 8px 0 0; }
  #scr-menu .logo { margin-bottom: 10px; }
  #scr-menu .logo .t1 { font-size: clamp(32px, 9vw, 42px); }
  #scr-menu .logo .t2 { font-size: 12px; }
  #scr-menu .partner-card { padding: 6px 0 14px; }
  /* Stable unzoomed dimensions also avoid the device ResizeObserver feeding its
     own fractional zoom back into layout on narrow screens. */
  #scr-menu .partner-card .stage#menu-partner { width: 156px !important; height: 120px !important; }
  #scr-menu .partner-card .dv.rq-dv { zoom: 1 !important; }
  #scr-menu .menu-right { flex: 0 0 auto; width: 100%; padding: 4px 0 12px; overflow: visible; gap: 12px; }
  #scr-menu .menu-right > .box { padding: 12px; max-height: none; overflow: visible; }
  #scr-menu .menu-list { gap: 7px; }
  #scr-menu .menu-item { min-height: 60px; padding: 10px 14px; font-size: 19px; }
  #scr-menu .menu-item.play { min-height: 72px; }
  #scr-menu .menu-item small { font-size: 12px; line-height: 1.5; }
  #scr-menu .credits-line { position: static; width: 100%; padding: 8px 2px; background: none; flex: 0 0 auto; font-size: 12px; line-height: 1.7; }
  #topbar { top: env(safe-area-inset-top); left: 0; right: 0; padding: 6px; gap: 3px; flex-wrap: nowrap; border-bottom: 1px solid #355780; border-radius: 0; }
  #topbar .btn { flex: 0 0 40px; width: 40px; min-width: 40px; min-height: 40px; padding: 5px; }
  #topbar .pill { min-width: 0; flex-shrink: 1; min-height: 36px; font-size: 13px; padding: 4px; gap: 3px; }
  #scr-dex, #scr-inc, #scr-upgrades, #scr-goals { padding-top: calc(82px + env(safe-area-inset-top)); }
  .split, .rqe-main { grid-template-columns: minmax(0,1fr) !important; gap: 12px; }
  #scr-dex .split { display: flex; flex-direction: column; }
  #dex-detail { min-width: 0; order: -1; scroll-margin-top: 64px; }
  .scr .tabs, .scr .gen-tabs, .scr .filters, .rqe-tabs {
    display: flex; flex-wrap: nowrap; overflow-x: auto; max-width: 100%; gap: 5px; padding-bottom: 6px;
  }
  .scr .tabs > *, .scr .gen-tabs > *, .scr .filters > *, .rqe-tab { flex: 0 0 auto; min-height: 44px; }
  #scr-settings #set-tabs { flex-wrap: nowrap; }
  .scr .cards { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; width: 100%; }
  .scr .cards > * { width: 100% !important; min-width: 0 !important; max-width: 100%; }
  .scr .card { padding: 12px; }
  #digi-no, #lvl-reroll { width: 100%; }
  .set-body, .goals-body, .over-scroll, .rqe-chips { max-height: none !important; overflow: visible !important; }
  .set-row { flex-wrap: wrap; gap: 10px; }
  .set-row > * { min-width: 0; max-width: 100%; }
  body .rq-page .keys { grid-template-columns: minmax(0, 1fr); gap: 4px; font-size: 14px; }
  body .rq-page .keys > span { margin-bottom: 12px; }
  .scr.rq-page { z-index: 60; }
  .rq-mtx { max-width: 100%; font-size: 12px; }
  .rq-mtx td, .rq-mtx th { padding: 3px; }
  .rqe-big { flex-wrap: wrap; }
  .rqe-card { width: 84px; }
  .rqe-arrow { width: 14px; }
  .rqe-kv { grid-template-columns: auto minmax(0, 1fr); overflow-wrap: anywhere; }
  .inc-foot, .inc-slots, .upg { grid-template-columns: minmax(0, 1fr); }
  .upg-head { align-items: flex-start; }
  .sound-panel { left: 12px; right: 12px; width: auto; max-width: none; }
  .tapstart .logo .t1 { font-size: clamp(32px, 9vw, 44px); }
  .tapstart .boot-in, .boot .boot-in { width: 100%; padding: 24px; }
  /* Explicitly separate the skill rail from both thumb zones. Full safe-area
     values, not upstream's capped 28px value, are used for portrait controls. */
  body #compact-skills { bottom: calc(164px + env(safe-area-inset-bottom)); gap: 6px; max-width: calc(100vw - 24px); }
  body #compact-skills .slot { width: 58px; height: 62px; box-shadow: none; }
  body #compact-skills .detail { bottom: 100%; margin-bottom: 10px; left: 50%; font-size: 13px; max-height: 32dvh; overflow-y: auto; pointer-events: auto; }
  body #rq-q { right: max(12px, env(safe-area-inset-right)); bottom: calc(24px + env(safe-area-inset-bottom)); width: calc(100vw - 174px); max-width: 230px; flex-wrap: wrap; gap: 5px; }
  body #rq-q .act { width: 100%; min-height: 76px; padding: 10px; }
  body #rq-q .act b { font-size: 15px; }
  body #rq-q .act span { font-size: 12px; margin-top: 4px; }
  body #rq-q .cyc { min-height: 36px; width: 100%; padding: 5px; }
  body.rq-in-run .toast, body.rq-in-run .ach-pop,
  body.rq-in-run:has(#ach-pop:not(.hidden)) .toast {
    bottom: calc(242px + env(safe-area-inset-bottom)); left: 12px; right: auto;
    max-width: calc(100vw - 24px); pointer-events: none; font-size: 13px;
  }
  body.rq-in-run:has(#ach-pop:not(.hidden)) .toast { bottom: calc(254px + env(safe-area-inset-bottom) + var(--rq-ach-h, 64px)); }
  /* Timed announcements must not sit on reward choices or modal close buttons. */
  body.rq-in-run:has(.scr:not(.hidden)) .toast,
  body.rq-in-run:has(.scr:not(.hidden)) .ach-pop { z-index: 19; }
}
#portrait-stick { display: none; }
@media (max-width: 600px) and (orientation: portrait) {
  #portrait-stick:not([hidden]) {
    display: grid; place-items: center; position: fixed; z-index: 15;
    left: max(12px, env(safe-area-inset-left)); bottom: calc(22px + env(safe-area-inset-bottom));
    width: 136px; height: 136px; padding: 0; touch-action: none; user-select: none;
    color: #b9d9ef; border: 1px solid #547fa5; border-radius: 50%;
    background: radial-gradient(circle, #153662b8 0 28%, #0a1939b8 29% 55%, #24487570 56% 57%, #09162d88 58%);
    font: 12px/1.4 var(--rq-font);
  }
  #portrait-stick::before, #portrait-stick::after { content: ""; position: absolute; background: #6685a0; opacity: .4; }
  #portrait-stick::before { width: 1px; height: 106px; }
  #portrait-stick::after { width: 106px; height: 1px; }
  #portrait-stick .knob { position: absolute; z-index: 1; width: 44px; height: 44px; border: 2px solid #fff0b4; border-radius: 50%; background: #e7b437; box-shadow: 0 3px 0 #805b21; pointer-events: none; }
  #portrait-stick .label { position: absolute; bottom: 5px; pointer-events: none; }
  #portrait-stick:focus-visible { outline: 2px solid #6fe3ff; outline-offset: 4px; }
}
