/* Phone and small-screen layout, shared by every page. Loaded after the page's own CSS.
   Desktop (wider than 760px) is unchanged except for the `scrolls` rules below. */

/* ---- all widths: pages that are longer than the window must be able to scroll.
   style.css hides overflow on html/body, which cut off long game lists. Add
   class="scrolls" to <html> on pages that are not a fixed-size board. */
html.scrolls {
  height: auto;
  min-height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  /* the header is fixed on wide screens: keep what is scrolled into view (a focused field,
     a clicked button) clear of it instead of hidden underneath */
  scroll-padding-top: 110px;
}

html.scrolls body {
  height: auto;
  min-height: 100%;
  overflow: visible;
}

html.scrolls header {
  background: #ffffffd9;
  backdrop-filter: blur(6px);
}

/* #probabilities uses white-space: pre, so the template's indentation inside the legend
   became real whitespace wider than the legend and pushed the page sideways */
#probabilities .legend-holder, #probabilities .legend {
  white-space: normal;
}

/* ---- header (all widths): logo on the left, navigation on the right. The navigation wraps
   inside its own box, so adding links never makes the fixed header taller and cover the page. */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header > div {
  display: block;
}

header .logoHolder {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
}

header .joinDiscordHolder {
  float: none;
  margin: 0 16px;
  flex: 0 0 auto;
}

header .pages {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  margin-right: 12px;
}

header .page-link {
  margin: 0 4px;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 17px;
  white-space: nowrap;
}

.logout-form {
  display: inline;
  margin: 0;
}

button.page-link {
  background: none;
  border: none;
  font-family: inherit;
  cursor: pointer;
}

/* the form container is 646px wide plus 120px of padding: never wider than the screen */
overlay .container {
  width: min(646px, calc(100vw - 120px));
}

/* the board must not scroll the page while a piece is being dragged */
#chessboard {
  touch-action: none;
}

.coming-soon {
  max-width: 480px;
  margin: 80px auto;
  padding: 0 16px;
  text-align: center;
  line-height: 1.5;
}

/* not enough room for the Discord button next to the navigation */
@media (max-width: 900px) {
  header .joinDiscordHolder {
    display: none;
  }
}

@media (max-width: 760px) {
  /* ---- page frame */
  html, html.scrolls {
    height: auto;
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    scroll-padding-top: 0; /* the header is not fixed on phones */
  }

  body, html.scrolls body {
    height: auto;
    min-height: 100%;
    overflow: visible;
  }

  /* the header joins the page flow instead of floating over the content */
  header, html.scrolls header {
    position: relative;
    width: auto;
    background: none;
    backdrop-filter: none;
    display: block;
  }

  main {
    margin: 8px 8px 24px;
    display: block;
  }

  /* ---- header and navigation: logo on top, links in a 2 x 2 grid of big tap targets */
  .logoHolder {
    margin: 10px 12px 4px;
    display: flex;
    align-items: center;
  }

  .logoHolder .logo {
    width: 40px;
    height: 40px;
    background-size: 40px;
  }

  .logoHolder .brandName {
    font-size: 24px;
    margin-left: 12px;
    vertical-align: middle;
  }

  .joinDiscordHolder {
    display: none;
  }

  header .pages {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    margin: 0 8px;
  }

  header .page-link {
    margin: 0;
    padding: 4px;
    min-height: 44px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    text-align: center;
    border-radius: 6px;
  }

  .logout-form {
    display: block;
  }

  .logout-form .page-link {
    width: 100%;
  }

  .page-link:active {
    background: #fff8;
  }

  /* ---- full-page forms (login, onboarding): the "overlay" becomes a normal page */
  overlay, html.scrolls overlay {
    position: static;
  }

  overlay .container {
    width: auto;
    height: auto;
    min-height: 60vh;
    box-sizing: border-box;
    padding: 16px 12px 32px;
  }

  overlay form, #onboard-form, #confirmation, #step-importing, #step-done {
    margin-left: 0;
    margin-right: 0;
  }

  #login-form {
    margin: 1.5em 0;
  }

  .notice {
    margin: 1em 0 0;
  }

  input[type="text"], input[type="email"] {
    box-sizing: border-box;
    font-size: 16px; /* anything smaller makes iOS zoom the page when a field is focused */
    min-height: 44px;
  }

  .action-button-small, #loginButton button, #onboard-form button {
    min-height: 44px;
    box-sizing: border-box;
  }

  .skip a {
    display: inline-block;
    padding: 12px;
  }

  /* ---- games list: one card per game instead of a five-column table */
  #tableHolder {
    width: 100%;
  }

  .custom-table, .custom-table tbody {
    display: block;
    width: 100% !important;
    box-shadow: none;
  }

  .custom-table thead {
    display: none;
  }

  .custom-table tr {
    display: block;
    margin: 10px 0;
    padding: 6px 12px;
    border-radius: 8px;
    background: #fffa;
    box-shadow: 0 1px 4px #0002;
  }

  .custom-table td {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 0;
    text-align: right;
    border-bottom: 1px solid #0001;
  }

  .custom-table td:last-child {
    border-bottom: none;
  }

  .custom-table td::before {
    content: attr(data-label);
    color: #666;
    text-align: left;
  }

  .custom-table td a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
  }

  .custom-table tr.unreadable td {
    display: block;
    text-align: left;
  }

  .custom-table tr.unreadable td::before {
    content: none;
  }

  .empty-state {
    margin: 32px auto;
  }

  /* ---- board pages (play, analysis): the board takes the width of the screen */
  board-area, analysis-area {
    display: block;
    height: auto;
    padding: 0;
    width: auto;
  }

  /* board size: the width of the screen, but never taller than the screen when the
     phone is on its side */
  :root {
    --board-size: min(calc(100vw - 16px), max(280px, calc(100vh - 24px)));
  }

  #chessboard-holder {
    width: var(--board-size) !important;
    height: var(--board-size) !important;
    margin: 8px auto 64px; /* room underneath for the button row */
  }

  #chessboard {
    width: calc(var(--board-size) - 16px) !important;
    top: 8px;
    left: 8px;
  }

  #victoryOverlay {
    width: calc(var(--board-size) - 16px);
    height: calc(var(--board-size) - 16px);
    top: 8px;
    left: 8px;
    background-size: contain;
  }

  /* analysis: the player names sit in 23 px bands above and below the board */
  body.analysis-page #chessboard-holder {
    height: calc(var(--board-size) + 58px) !important;
    margin-bottom: 8px;
  }

  body.analysis-page #chessboard {
    width: calc(var(--board-size) - 6px) !important;
    top: 30px;
    left: 3px;
  }

  /* play: controls become a row of 44 px buttons under the board */
  #buttons {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    bottom: auto;
    display: flex;
    justify-content: center;
    gap: 4px;
    padding-top: 6px;
  }

  #buttons button {
    width: 44px;
    height: 44px;
    margin: 0;
    opacity: 60%;
  }

  #switch-color {
    width: 30px !important;
    height: 30px !important;
    margin: 7px !important;
  }

  #thinking, #fen, #pgn, #custom-start-input, #analysis {
    width: auto;
    box-sizing: border-box;
    margin-left: 8px;
    margin-right: 8px;
  }

  #fen span {
    word-break: break-all;
  }

  #pgn .ply {
    display: inline-block;
    padding: 6px 4px; /* easier to tap */
  }

  #probabilities {
    box-sizing: border-box;
    padding: 6px;
  }

  #probabilities-holder {
    height: auto;
    min-height: 220px;
  }

  #probabilities-overlay {
    line-height: 1.5; /* was 220px, which spread a wrapped sentence over the whole card */
  }

  #probabilities canvas {
    width: 100% !important;
    height: auto !important;
    max-width: calc(100vw - 80px);
  }

  #model-progress {
    width: auto;
    margin: 0 8px 12px;
  }

  /* modal windows on the play page */
  overlay[style*="display: block"], overlay[style*="display:block"] {
    z-index: 300; /* above the header, or its logo catches taps meant for the close button */
    position: fixed;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #close-overlay {
    position: fixed;
    right: 8px;
    top: 8px;
    width: 44px;
    height: 44px;
    font-size: 36px;
    line-height: 44px;
    background: #fffc;
    z-index: 200;
  }

  overlay h1 {
    font-size: 26px;
  }

  textarea, #custom-start-input {
    font-size: 16px;
  }

  /* analysis: the evaluation bar lies along the top of the board */
  #board-wrap {
    display: block;
  }

  #evalBar {
    width: var(--board-size);
    height: 22px;
    margin: 4px auto 0;
    flex-direction: row;           /* White's share grows from the left */
  }

  #evalBar.flipped {
    flex-direction: row-reverse;
  }

  #evalBar .white {
    height: auto;
    width: var(--white);
  }

  #evalBar .label {
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    padding: 0 6px;
  }

  #evalBar:not(.flipped) .label { left: 0; right: auto; text-align: left; }
  #evalBar.flipped .label { left: auto; right: 0; text-align: right; }

  #review-summary {
    width: auto;
    margin-left: 8px;
    margin-right: 8px;
  }

  #game-graph, #moves {
    width: auto;
    margin-left: 8px;
    margin-right: 8px;
  }

  #graph {
    height: 90px;
  }

  #move-list {
    max-height: 180px;
  }

  #move-list .ply {
    padding: 8px 7px; /* a 44px-ish target */
  }

  #engineToggle {
    min-height: 44px;
    padding: 0 14px;
  }

  /* play: level, undo, resign sit under the button row */
  #game-controls {
    margin: 0 8px;
  }

  #game-controls select,
  #game-controls button {
    min-height: 44px;
  }

  /* analysis: previous/next/calculate as big buttons */
  #controls {
    display: flex;
    gap: 8px;
    margin: 8px;
  }

  #controls button {
    flex: 1;
    min-height: 44px;
    font-size: 15px;
  }
}

@media (max-width: 380px) {
  .logoHolder .brandName {
    font-size: 20px;
  }

  header .page-link {
    font-size: 15px;
  }
}

@media (max-width: 330px) {
  header .pages {
    grid-template-columns: repeat(2, 1fr);
  }
}
