/*
 * DOM board layout.
 *
 * The board is a square that grows to fill whatever space it is given, with
 * the two komadai (captured-piece stands) in the columns either side of it.
 * That is what reclaims the empty margins the iframe left behind: on a wide
 * screen the board grows until it runs out of height, and the hands sit in
 * the space that used to be blank.
 */

/* Don't rely on a global reset being present. */
.sb-root, .sb-root *, #game-container.is-dom {
  box-sizing: border-box;
}

/* #game-container centres the Godot iframe; the DOM board wants the full box. */
#game-container.is-dom {
  align-items: stretch;
  justify-content: stretch;
  padding: 8px;
}

.sb-root {
  display: grid;
  /* Content-sized columns, centred as a group, so the komadai sit against
     the board instead of being pushed out to the edges of the page by a
     1fr middle column. */
  grid-template-columns: auto auto auto;
  justify-content: center;
  align-items: center;
  gap: 12px;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.sb-board-wrap {
  /* Square, as large as the shorter axis allows, centred in its column. */
  aspect-ratio: 1 / 1;
  height: 100%;
  max-height: 100%;
  max-width: 100%;
  margin: 0 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sb-canvas {
  display: block;
  /* Width and height are set in JS from the measured box and devicePixelRatio. */
  touch-action: manipulation;
}

/* ---- hands ---------------------------------------------------------- */

.sb-hand {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 72px;
  max-width: 96px;
  padding: 8px 6px;
  border: 1px solid var(--paper-edge, #D8C48C);
  border-radius: 6px;
  background: var(--paper-deep, #E7D8AF);
}

.sb-hand--opponent {
  /* The opponent's stand belongs on their right, which is the viewer's left. */
  align-self: start;
}

.sb-hand--own {
  align-self: end;
}

.sb-hand__label {
  /* Sentence case, not uppercase: "Opponent" fits the stand's width where
     "OPPONENT" has to be truncated to "OPPON...". */
  font-size: 0.62rem;
  letter-spacing: 0;
  color: var(--ink-soft, #5C5040);
  text-align: center;
  /* Never let the caption decide how wide the komadai is — "OPPONENT" is
     wider than the pieces, which made that stand wider than "YOU" and pushed
     the board off-centre. */
  /* width:0 + min-width:100% is the trick that matters: it makes the label
     contribute nothing to the stand's max-content width, then stretches it to
     whatever width the pieces settled on. Without it "OPPONENT" is wider than
     the pieces and that stand ends up wider than "YOU". */
  width: 0;
  min-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sb-hand__pieces {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;          /* a full komadai is 7 kinds — it must fold, not clip */
  align-content: flex-start;
  gap: 4px;
  align-items: center;
  max-height: 100%;
  min-height: 0;
}

.sb-hand__slot {
  position: relative;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sb-hand__img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.sb-hand__count {
  position: absolute;
  right: 0;
  bottom: 0;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1;
  padding: 1px 3px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--ink, #2A2118);
  pointer-events: none;
}

.sb-hand__empty {
  color: var(--ink-soft, #5C5040);
  opacity: 0.6;
  font-size: 0.9rem;
  padding: 6px 0;
}

/* ---- interaction (Step 7) -------------------------------------------- */

.sb-board-wrap {
  position: relative;   /* anchors the promotion overlay */
}

.sb-hand__slot.is-droppable {
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.12s ease, transform 0.12s ease;
}

.sb-hand__slot.is-droppable:hover,
.sb-hand__slot.is-droppable:focus-visible {
  background: rgba(44, 74, 99, 0.14);
  outline: none;
}

.sb-hand__slot.is-armed {
  background: rgba(44, 74, 99, 0.28);
  box-shadow: inset 0 0 0 2px var(--indigo, #2C4A63);
}

/* ---- promotion chooser ------------------------------------------------ */

.sb-promo {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(42, 33, 24, 0.45);
  z-index: 5;
}

.sb-promo[hidden] {
  display: none;
}

.sb-promo__box {
  background: var(--paper, #F2E8D2);
  border: 2px solid var(--wood-deep, #43301C);
  border-radius: 8px;
  padding: 14px 16px;
  box-shadow: 0 10px 30px -12px rgba(43, 30, 15, 0.55);
  text-align: center;
  max-width: 90%;
}

.sb-promo__title {
  margin: 0 0 10px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink, #2A2118);
}

.sb-promo__choices {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.sb-promo__btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: auto;
  padding: 8px 12px;
  border: 2px solid var(--paper-edge, #D8C48C);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font: inherit;
  font-size: 0.85rem;
  color: var(--ink, #2A2118);
}

.sb-promo__btn:hover,
.sb-promo__btn:focus-visible {
  border-color: var(--seal, #A3382A);
  outline: none;
}

.sb-promo__img {
  width: 52px;
  height: 52px;
  object-fit: contain;
}

@media (max-height: 520px) {
  .sb-promo__img {
    width: 38px;
    height: 38px;
  }

  .sb-promo__box {
    padding: 8px 10px;
  }
}

/* ---- responsive ------------------------------------------------------
 *
 * The layout keys off ORIENTATION, not a width breakpoint, because what
 * matters is which axis is scarce.
 *
 *   landscape — height is scarce. Size the board from the height and put the
 *               komadai beside it, so the whole board is on screen without
 *               scrolling.
 *   portrait  — width is scarce. Size the board from the width and stack the
 *               komadai above and below.
 *
 * A width breakpoint got this wrong on a phone held sideways: at 844px wide
 * it stacked the hands and sized the board from the width, producing a 568px
 * board on a 390px-tall screen.
 */

@media (orientation: landscape) {
  .sb-root {
    grid-template-columns: auto auto auto;
    grid-template-rows: minmax(0, 1fr);
    justify-content: center;
    height: 100%;
  }

  .sb-board-wrap {
    height: 100%;
    width: auto;
    max-width: 100%;
  }

  .sb-hand {
    flex-direction: column;
    /* Stretch to the row so the piece list has a definite height to wrap
       against; a column-direction flex container cannot wrap without one. */
    align-self: stretch;
    height: 100%;
    min-width: 0;
    max-width: none;
    width: max-content;
  }

  .sb-hand__pieces {
    flex-direction: column;
    flex-wrap: wrap;
    flex: 1 1 auto;
    min-height: 0;
  }

  /* Keep each player's pieces nearest their own end of the board. */
  .sb-hand--opponent .sb-hand__pieces {
    align-content: flex-start;
    justify-content: flex-start;
  }

  .sb-hand--own .sb-hand__pieces {
    align-content: flex-end;
    justify-content: flex-end;
  }
}

@media (orientation: portrait) {
  .sb-root {
    /* Rows size to their content so the board can take the full width;
       the page scrolls if the hands push it past the fold. */
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    justify-content: stretch;
    /* Without this the `auto` rows stretch to fill any leftover height, so an
       empty komadai became a 250px slab and pushed the board down the page. */
    align-content: start;
    height: auto;
  }

  .sb-board-wrap {
    width: 100%;
    height: auto;
    max-height: none;
  }

  .sb-canvas {
    max-width: 100%;
  }

  .sb-hand {
    /* Label ABOVE the pieces, not beside them: the label was taking ~70px of
       a 390px phone, which was enough to push the seventh piece off the edge. */
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    width: 100%;
    min-width: 0;
    max-width: none;
    height: auto;
  }

  .sb-hand--opponent,
  .sb-hand--own {
    align-self: stretch;
  }

  .sb-hand__label {
    text-align: left;
  }

  .sb-hand__pieces {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    max-height: none;
    width: 100%;
  }
}

/* ---- small screens ---------------------------------------------------
 * Trade chrome for board. Hand slots scale with the scarce axis so seven
 * pieces in hand still fit beside a short landscape board.
 */

@media (max-width: 820px), (max-height: 520px) {
  #game-container.is-dom {
    padding: 2px;
  }

  .sb-root {
    gap: 6px;
  }

  .sb-hand {
    padding: 4px 3px;
    gap: 3px;
  }

  .sb-hand__label {
    font-size: 0.55rem;
  }
}

/* An empty komadai should still read as a stand rather than collapsing to a
   sliver too narrow for its own caption. Only where there is height to spare. */
@media (orientation: landscape) and (min-height: 521px) {
  .sb-hand {
    min-width: 3.4rem;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  /* On a phone held sideways the stands are barely wider than a piece, so the
     caption only truncates to "Oppo...". Position says it anyway — top stand is
     theirs, bottom stand is yours — and dropping it buys height for pieces. */
  .sb-hand__label {
    display: none;
  }

  .sb-hand {
    min-width: 0;
    max-width: none;
  }

  .sb-hand__slot {
    width: clamp(26px, 7.5vh, 48px);
    height: clamp(26px, 7.5vh, 48px);
  }
}

@media (orientation: portrait) and (max-width: 520px) {
  .sb-hand__slot {
    width: clamp(30px, 11vw, 48px);
    height: clamp(30px, 11vw, 48px);
  }
}
