/* RingoChess v153 · Responsive Pro
   Additional layout layer; does not change chess rules, navigation, review
   classification, saved games, or online networking. */
:root { --r153-gutter: clamp(8px, 1.3vw, 18px); }

/* Guard rails common to all screen sizes. */
html, body { max-width: 100%; }
.app-shell, .topbar, .game-layout, .board-panel, .side-panel { min-width: 0; }
.game-layout :is(img, svg, video, iframe, canvas) { max-width: 100%; }
.game-layout :is(input, select, textarea) { min-width: 0; }
.game-layout .side-panel :is(pre, code) { overflow-wrap: anywhere; }
.game-layout .side-panel :is(.analysis-classifier,.analysis-card,.tool-panel,.game-classification-list) { min-width: 0; max-width: 100%; }

/* Tablets, landscape phones and small desktop windows: a scrollable document
   is safer than the legacy fixed-height iPad and desktop rules. */
@media (max-width: 1179px), (pointer: coarse) {
  html:not(.play-focus-active),
  html.ringo-v142-isolated,
  html.ringo-v142-isolated body:not(.play-focus-active),
  html body:not(.play-focus-active) {
    height: auto !important;
    max-height: none !important;
    overflow-x: clip !important;
    overflow-y: auto !important;
    overscroll-behavior-y: auto !important;
  }
  html body:not(.play-focus-active) .app-shell {
    display: block !important;
    height: auto !important;
    min-height: 100dvh !important;
    max-height: none !important;
    overflow: visible !important;
    padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)) !important;
  }
  html body:not(.play-focus-active) .game-layout[data-active-view] {
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
    overflow: visible !important;
  }
  html body:not(.play-focus-active) .game-layout[data-active-view] > .board-panel {
    position: relative !important;
    top: auto !important;
    bottom: auto !important;
    max-height: none !important;
    overflow: visible !important;
  }
  html body:not(.play-focus-active) .game-layout[data-active-view] > .side-panel {
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
    overflow-y: visible !important;
    overflow-x: visible !important;
    padding-bottom: max(16px, env(safe-area-inset-bottom)) !important;
  }
}

/* iPhone, compact Android phones, iPad portrait, split-view and narrow windows. */
@media (max-width: 979px) {
  :root {
    --board-size: min(calc(100vw - 22px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 138px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 810px) !important;
  }
  html body:not(.play-focus-active) .game-layout,
  html body:not(.play-focus-active) .game-layout[data-active-view] {
    display: grid !important;
    grid-template-columns: minmax(0,1fr) !important;
    gap: 10px !important;
    align-items: start !important;
  }
  html body:not(.play-focus-active) .game-layout > .board-panel,
  html body:not(.play-focus-active) .game-layout > .side-panel {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    justify-self: stretch !important;
  }
  html body:not(.play-focus-active) .board-stage,
  html body:not(.play-focus-active) .board-shell,
  html body:not(.play-focus-active) .board-panel .board-topbar {
    width: min(100%,var(--board-size)) !important;
    max-width: 100% !important;
  }
  html body:not(.play-focus-active) .board-stage,
  html body:not(.play-focus-active) .board-shell { margin-inline:auto !important; }
  html body:not(.play-focus-active) .board-shell { padding: 4px !important; }
  html body:not(.play-focus-active) .board-panel .board-bottom-viewer {display:none!important;}
  html body:not(.play-focus-active) .game-layout[data-active-view="analysis"] .side-panel > .analysis-card.ringo148-postgame-focus {order:-100!important;}
  .game-layout .game-classification-list,
  .game-layout .ringo150-classified-moves {max-width:100%!important;}
}

/* Tablet/PC landscape and mid-sized free-form windows. Two columns only when
   there really is enough width AND vertical room for the full chessboard. */
@media (min-width: 980px) and (max-width: 1179px) and (min-height: 580px) {
  :root {
    --board-size: min(calc(100vw - 365px - 28px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 112px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 780px) !important;
  }
  html body:not(.play-focus-active) .game-layout,
  html body:not(.play-focus-active) .game-layout[data-active-view] {
    display:grid !important;
    grid-template-columns: minmax(0, calc(var(--board-size) + 10px)) minmax(315px,1fr) !important;
    gap: 12px !important;
    align-items:start !important;
  }
  html body:not(.play-focus-active) .board-stage,
  html body:not(.play-focus-active) .board-shell,
  html body:not(.play-focus-active) .board-panel .board-topbar {
    width: min(100%,var(--board-size)) !important;
    max-width:100%!important;
  }
  html body:not(.play-focus-active) .board-panel .board-bottom-viewer {display:none!important;}
}

/* Large pointer-based desktops: keep the successful v142 independent sidebar,
   but use the true available window size rather than a hard-coded 900px board. */
@media (min-width: 1180px) and (min-height: 700px) and (pointer: fine) {
  :root {
    --board-size: min(calc(100vw - 472px - 2.2rem),
      calc(100dvh - 124px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 940px) !important;
  }
  html body:not(.play-focus-active) .app-shell {
    width: min(1640px,100%) !important;
    max-width: 100% !important;
    padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)) !important;
  }
  html body:not(.play-focus-active) .game-layout,
  html body:not(.play-focus-active) .game-layout[data-active-view] {
    display:grid !important;
    grid-template-columns:minmax(0,1fr) minmax(340px,min(34vw,460px)) !important;
    gap:clamp(10px,1.3vw,18px) !important;
  }
  html body:not(.play-focus-active) .board-stage,
  html body:not(.play-focus-active) .board-shell,
  html body:not(.play-focus-active) .board-panel .board-topbar {
    width:min(100%,var(--board-size)) !important;
    max-width:100% !important;
  }
}

/* Header wraps without losing the menu or pushing the board beyond the viewport. */
@media (max-width: 760px) {
  html body:not(.play-focus-active) .topbar {
    display:grid !important;
    grid-template-columns:minmax(0,1fr) auto !important;
    align-items:center !important;
    column-gap:6px !important;
    row-gap:5px !important;
    margin-bottom:7px !important;
  }
  html body:not(.play-focus-active) .topbar > div:first-child {
    grid-column:1;grid-row:1; min-width:0; gap:4px;
  }
  html body:not(.play-focus-active) .topbar-actions {
    grid-column:2;grid-row:1;justify-self:end;min-width:0;gap:4px;
  }
  html body:not(.play-focus-active) .top-review-viewer {
    grid-column:1/-1 !important;grid-row:2 !important;width:100% !important;
    min-width:0 !important;max-width:100% !important;
    padding:3px 6px !important;gap:4px !important;
  }
  html body:not(.play-focus-active) .top-review-viewer .viewer-controls {
    display:flex;flex-wrap:nowrap;min-width:0;justify-content:center;
  }
  html body:not(.play-focus-active) .top-review-viewer .viewer-controls button {
    flex:0 0 28px!important;width:28px!important;min-width:28px!important;
    height:28px!important;min-height:28px!important;padding:2px!important;
  }
  html body:not(.play-focus-active) .top-review-viewer .viewer-status {min-width:0;max-width:calc(100% - 230px);font-size:10px;}
  html body:not(.play-focus-active) .topbar h1 {font-size:12px!important;letter-spacing:.11em!important;white-space:nowrap;}
  html body:not(.play-focus-active) .topbar .version {font-size:9px!important;white-space:nowrap;}
  html body:not(.play-focus-active) .toplinks a {padding:5px 9px!important;min-height:32px!important;font-size:12px!important;}
  html body:not(.play-focus-active) .main-menu-dropdown summary {min-height:34px!important;min-width:34px!important;}
  html body:not(.play-focus-active) .main-menu-dropdown .mode-nav {max-width:min(95vw,440px);max-height:min(75dvh,610px);overflow-y:auto;}
}

/* Very small phones: keep controls usable and prevent sideways clipping. */
@media (max-width: 390px) {
  :root {
    --board-size:min(calc(100vw - 16px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 134px - env(safe-area-inset-top) - env(safe-area-inset-bottom))) !important;
  }
  html body:not(.play-focus-active) .topbar .version {display:none!important;}
  html body:not(.play-focus-active) .top-review-viewer .viewer-status {display:none!important;}
  html body:not(.play-focus-active) .top-review-viewer {justify-content:center!important;}
  html body:not(.play-focus-active) .board-shell {padding:3px!important;}
  html body:not(.play-focus-active) .game-layout #ringoBoardReplayControls {
    display:flex!important;flex-wrap:wrap!important;justify-content:center!important;
    gap:4px!important;padding:5px 3px!important;max-width:100%!important;
  }
  html body:not(.play-focus-active) .game-layout #ringoBoardReplayControls > .ringo-board-replay-button {
    flex:0 0 40px!important;width:40px!important;min-width:40px!important;max-width:40px!important;
    height:40px!important;min-height:40px!important;max-height:40px!important;
  }
  html body:not(.play-focus-active) .game-layout #ringoBoardReplayControls > #ringo149ResetBoardBtn {
    flex:0 0 40px!important;width:40px!important;min-width:40px!important;max-width:40px!important;
    height:40px!important;min-height:40px!important;max-height:40px!important;
  }
  html body:not(.play-focus-active) .game-layout #ringoBoardReplayControls > #ringoGlobalFlipBoard {
    flex:0 0 58px!important;width:58px!important;min-width:58px!important;max-width:58px!important;
    height:40px!important;min-height:40px!important;max-height:40px!important;
  }
  html body:not(.play-focus-active) .game-layout[data-active-view="analysis"] .analysis-classifier .ringo150-classified-moves .game-classification-jump {
    min-width:0!important;width:100%!important;grid-template-columns:max-content minmax(24px,1fr) 29px max-content!important;
    gap:3px!important;padding:3px!important;
  }
}

@media (max-height: 579px) and (orientation:landscape) {
  :root {
    --board-size:min(calc(100vw - 22px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      calc(100dvh - 100px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 470px) !important;
  }
}

/* Full feature access with keyboard zoom and reduced-motion preferences. */
@media (prefers-reduced-motion:reduce) {
  html {scroll-behavior:auto!important;}
  .game-layout .eval-bar-white {transition:none!important;}
}
