.mobileControl,
#resultSolution.mobileControl {
  display: none;
}

/* The outer page is one viewport. Sheets can still scroll when the keyboard
   or larger accessibility text reduces their usable height. */
@media (max-width: 699px),
  (pointer: coarse) and (orientation: portrait) and (max-width: 1024px),
  (pointer: coarse) and (max-height: 540px) {
  :root {
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
    --glass: #172f28e8;
    --brass: #d2b57e;
    --cream: #fff1d5;
    background: #163129;
  }
  html,
  body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }
  body {
    min-height: 0;
  }
  main {
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0;
    isolation: isolate;
    overflow: hidden;
    position: relative;
    background: #172e26;
  }
  .brandbar {
    position: absolute;
    z-index: 4;
    top: calc(var(--safe-t) + 6px);
    left: calc(var(--safe-l) + 12px);
    right: calc(var(--safe-r) + 12px);
    padding: 0;
    max-width: none;
    border: 0;
    color: var(--cream);
    pointer-events: none;
  }
  .brandbar a,
  .brandbar button {
    pointer-events: auto;
  }
  .brandbar .brand-logo {
    min-height: 44px;
    text-shadow: 0 1px 5px #071b16;
  }
  .brandbar .textButton,
  .brandbar .icon,
  #pickLevel,
  #reset {
    min-height: 44px;
    border: 1px solid #c7ad7b70;
    border-radius: 14px;
    color: var(--cream);
    background: var(--glass);
    box-shadow: 0 2px 8px #06171028;
  }
  .brandbar .textButton {
    padding: 6px 12px;
  }
  main:has(#play:not([hidden])) .brandbar {
    left: auto;
  }
  main:has(#play:not([hidden])) .brandbar :is(#language, #lobbyLink) {
    display: none;
  }
  .mobileControl {
    display: block;
  }
  #play {
    height: 100%;
    width: 100%;
    max-width: none;
    margin: 0;
    padding: calc(var(--safe-t) + 4px) var(--safe-r) calc(var(--safe-b) + 6px)
      var(--safe-l);
    display: grid;
    align-items: stretch;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    gap: 0;
    background:
      linear-gradient(
        180deg,
        #10291fea,
        #183129c9 26%,
        #19372bd4 78%,
        #10271fee
      ),
      url("art/midnight.webp") center/cover;
    color: var(--cream);
  }
  #pickLevel {
    display: grid;
    place-items: center;
    position: absolute;
    z-index: 2;
    top: calc(var(--safe-t) + 6px);
    left: calc(var(--safe-l) + 12px);
    width: 44px;
    padding: 0;
  }
  .tiles {
    width: 7px;
    height: 7px;
    background: #ecd3a6;
    border-radius: 1.5px;
    box-shadow:
      9px 0 #ecd3a6,
      0 9px #ecd3a6,
      9px 9px #ecd3a6;
    transform: translate(-4.5px, -4.5px);
  }
  .levelHead {
    grid-area: 1 / 1 / 2 / -1;
    min-width: 0;
    width: 100%;
    padding: 3px 66px 5px;
    min-height: 51px;
    align-self: stretch;
    display: flex;
    justify-content: center;
    text-align: center;
  }
  .levelHead h1 {
    color: #fff1d5;
    font-size: clamp(20px, 5.6vw, 29px);
    line-height: 1.1;
    text-wrap: balance;
  }
  .levelHead .chapter {
    color: #d4bd88;
    font-size: 10px;
    line-height: 1.25;
    letter-spacing: 0.09em;
    margin-bottom: 3px;
  }
  .tableSide {
    display: contents;
  }
  .boardWrap {
    grid-area: 2 / 1 / 3 / -1;
    min-width: 0;
    min-height: 0;
    align-self: stretch;
    position: relative;
    z-index: 1;
  }
  #table {
    --fit-court: 1;
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
    filter: drop-shadow(0 3px 5px #06171074);
  }
  #rule {
    grid-area: 3 / 1 / 4 / -1;
    min-height: 0;
    margin: 0 14px;
    padding: 5px 2px 7px;
    font-size: clamp(12px, 3.3vw, 15px);
    line-height: 1.35;
    text-align: center;
    text-wrap: balance;
    color: #eadfc2;
  }
  .tableHud {
    grid-area: 4 / 1 / 5 / 2;
    display: flex;
    justify-content: space-between;
    gap: 6px;
    margin: 0 0 0 12px;
    min-width: 0;
    min-height: 44px;
    padding: 4px 13px 4px 5px;
    background: var(--glass);
    border: 1px solid #b9a27a65;
    border-radius: 15px;
    color: var(--cream);
  }
  #marbleStock {
    color: var(--cream);
    gap: 6px;
    font-size: 13px;
  }
  .marbleSlots {
    background: #0d211c;
    border-color: #8b7851;
  }
  .marbleSlot[data-state="current"] {
    outline-color: #f8e2a8;
  }
  .tableHud .score {
    color: #eee2c2;
    gap: 5px;
    font-size: 15px;
  }
  .tableHud .score i {
    color: #9fae9b;
  }
  .deskfoot {
    grid-area: 4 / 2 / 5 / 3;
    margin: 0 12px 0 8px;
    padding: 0;
    border: 0;
    min-height: 44px;
  }
  #reset {
    min-height: 44px;
    padding: 5px 13px;
  }
  #levels {
    display: none;
  }
  .deskfoot { gap: 5px; }
  #boardSolution {
    width: 44px; min-height: 44px; padding: 0;
    display: grid; place-items: center; border-radius: 14px;
    background: #ecd5a3; color: #253c30; border: 1px solid #cab17e;
  }
  #resultSolution.mobileControl { display: inline-flex; gap: 5px; background: #fbf3df; color: #384c39; border-color: #b5a27e; }
  #notice:not(:empty) {
    position: absolute;
    z-index: 3;
    bottom: calc(var(--safe-b) + 54px);
    left: 50%;
    transform: translateX(-50%);
    width: max-content;
    max-width: 92%;
    margin: 0;
    padding: 6px 12px;
    background: #fbf4df;
    border: 1px solid var(--brass);
    border-radius: 9px;
    color: #264636;
    text-align: center;
    pointer-events: none;
  }
  #result {
    bottom: 3%;
    max-height: 96%;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  dialog {
    max-height: calc(100% - var(--safe-t) - var(--safe-b) - 20px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .book-story {
    height: 100%;
    width: 100%;
    max-width: none;
    margin: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    background: #152b23;
    position: relative;
    display: grid;
    align-items: end;
    grid-template-columns: 1fr;
  }
  .book-story .plate {
    position: absolute;
    inset: 0;
    min-height: 0;
  }
  .plate img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: 51% 37%;
  }
  .book-story::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(
      180deg,
      #0c201bc9,
      transparent 19%,
      transparent 32%,
      #132b23ec 76%,
      #132b23 100%
    );
  }
  .storyCopy {
    position: relative;
    z-index: 1;
    align-self: end;
    color: #fff2d7;
    padding: 15px calc(var(--safe-r) + 22px) calc(var(--safe-b) + 20px)
      calc(var(--safe-l) + 22px);
  }
  .storyCopy .chapter {
    color: #dfc898;
  }
  .storyCopy h1 {
    font-size: clamp(28px, 8vw, 48px);
  }
  .storyCopy p {
    margin: 9px 0 17px;
    color: #eee5d0;
    font-size: clamp(14px, 4vw, 18px);
  }
  .storyActions .primary {
    background: #efdaaa;
    color: #1e372a;
    border-color: #efdaaa;
  }
  .storyActions .textButton,
  .storyActions .icon {
    color: #fff1d5;
  }
  .book-chapterGuide {
    height: 100%;
    width: 100%;
    max-width: none;
    margin: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    background: radial-gradient(ellipse at 55% 48%, #fffaf0, #eee3ca);
  }
  .guideCover {
    height: clamp(106px, 22dvh, 210px);
    min-height: calc(var(--safe-t) + 105px);
  }
  .guideHeading {
    padding: 6px 12px 9px calc(var(--safe-l) + 16px);
    max-width: calc(100% - 146px - var(--safe-r));
  }
  .guideHeading p {
    font-size: 10px;
    margin-bottom: 2px;
  }
  .guideHeading h1 {
    font-size: clamp(24px, 6.3vw, 35px);
  }
  .book-guideTabs {
    right: calc(var(--safe-r) + 10px);
    top: auto;
    bottom: 5px;
  }
  .guideInside {
    min-height: 0;
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "pitch" "demo" "route";
    padding: 7px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 8px)
      calc(var(--safe-l) + 12px);
    gap: 5px;
    align-items: stretch;
  }
  .guidePitch {
    margin: 0;
    text-align: center;
    align-self: center;
  }
  .book-guideText {
    font-size: clamp(12px, 3.5vw, 16px);
    line-height: 1.4;
    text-wrap: balance;
  }
  .guideFigure {
    position: relative;
    min-height: 0;
    align-self: stretch;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .book-guideBoard {
    width: 100%;
    height: 100%;
    min-height: 0;
    aspect-ratio: auto;
  }
  .book-guideReplay {
    min-height: 44px;
    background: #fbf4e4ec;
    border: 1px solid #c9b58c;
    border-radius: 12px;
    color: #355343;
  }
  .guideRoute {
    display: grid;
    gap: 5px;
  }
  .book-guideLevels {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
    border: 0;
  }
  .book-guideLevels button {
    min-width: 0;
    min-height: 52px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 2px;
    justify-items: center;
    align-content: center;
    text-align: center;
    background: #fff9ec88;
    border: 1px solid #cbbd9f;
    border-radius: 8px;
    padding: 4px 2px;
  }
  .book-guideLevels .routeIndex {
    font-size: 10px;
    line-height: 1;
  }
  .book-guideLevels .routeCopy {
    display: block;
  }
  .book-guideLevels .routeCopy strong {
    font-size: clamp(11px, 2.95vw, 15px);
    line-height: 1.12;
    display: block;
  }
  .book-guideLevels small {
    display: none;
  }
  .book-guideLevels .stageBest {
    width: auto;
    margin: 1px 0 0;
    padding: 0;
    justify-content: center;
  }
  .book-guideLevels .stageBest i {
    width: 9px;
    height: 9px;
  }
  .book-enterStage {
    min-height: 44px;
    margin: 0;
  }
}
/* A rotated phone keeps actions on the left, away from the narrow portrait board. */
@media (orientation: landscape) and (max-height: 540px) and (max-width: 699px),
  (orientation: landscape) and (max-height: 540px) and (pointer: coarse) {
  #play {
    grid-template-columns: minmax(160px, 36%) minmax(0, 1fr);
    grid-template-rows: 52px 1fr auto auto 1fr;
  }
  main:has(#play:not([hidden])) .brandbar {
    right: auto;
    left: calc(var(--safe-l) + 64px);
  }
  .levelHead {
    grid-area: 2 / 1;
    align-self: end;
    padding: 8px 12px;
    min-height: 0;
  }
  #rule {
    grid-area: 3 / 1;
    margin: 0 8px;
  }
  .tableHud {
    grid-area: 4 / 1;
    margin: 4px 8px;
    padding: 4px;
    gap: 2px;
  }
  .deskfoot {
    grid-area: 5 / 1;
    align-self: start;
    justify-self: center;
    margin: 4px 0;
  }
  .boardWrap {
    grid-area: 1 / 2 / -1 / 3;
  }
  #result {
    width: min(96%, 360px);
    bottom: 1%;
    max-height: 98%;
  }
  .awardTop {
    padding: 5px 14px;
  }
  .medals {
    min-height: 29px;
  }
  .medals canvas {
    width: 30px;
    height: 30px;
  }
  .medals canvas:nth-child(2) {
    width: 36px;
    height: 36px;
  }
  .awardSlip {
    padding-top: 5px;
  }
  .book-story .storyCopy {
    width: min(60%, 470px);
  }
  .guideCover {
    height: calc(var(--safe-t) + 100px);
    min-height: 0;
  }
  .guideInside {
    grid-template-columns: 1fr 1.15fr;
    grid-template-rows: auto 1fr;
    grid-template-areas: "demo pitch" "demo route";
  }
  .guideRoute {
    align-content: center;
  }
  .book-guideReplay {
    font-size: 11px;
    padding: 6px;
  }
}
