/* No text selection anywhere, also in dialogs (Safari needs the prefix). */
* {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

:root {
  --terracotta-surface: radial-gradient(
    circle at 10% 5%,
    #bf7466 0%,
    #944d55 48%,
    #5d303a 100%
  );
  --felt-green: #6bd66b;
  --light-red: #ff6b6b;
  --dark-yellow: #c99a06;
  --selection-green: #315c3c;
}

html,
body,
#game-shell,
#pixiCanvas {
  width: 100%;
  height: 100%;
}

html,
body {
  margin: 0;
  overflow: hidden;
  background: #944d55;
}

#pixiCanvas {
  display: block;
  touch-action: none;
}

.game-dialog {
  position: fixed;
  width: min(420px, calc(100vw - 32px));
  max-height: calc(
    100dvh -
    8px -
    min(env(safe-area-inset-top, 0px), 16.667dvh) -
    min(env(safe-area-inset-bottom, 0px), 16.667dvh)
  );
  overflow-y: auto;
  margin: 0;
  padding: 20px 20px 16px;
  border: 2px solid var(--felt-green);
  border-radius: 16px;
  color: #fff;
  background: var(--terracotta-surface);
  box-shadow: 0 10px 30px rgb(0 0 0 / 45%);
  font:
    16px Arial,
    sans-serif;
}

.game-dialog * {
  color: #fff;
}

/* A light scrim keeps the player's hand visible below the dialog. */
.game-dialog::backdrop {
  background: rgb(0 0 0 / 35%);
}

/* Hand text: one span per suit (handText.ts). */
.sheet-hand-suit {
  margin: 0 8px;
  white-space: nowrap;
}

.unplayed-cards-text {
  margin: 0 0 14px;
  font:
    700 20px Arial,
    sans-serif;
  text-align: center;
}

/* One row per suit; a long row may wrap. */
.unplayed-cards-text .sheet-hand-suit {
  display: block;
  margin: 0 0 6px;
  white-space: normal;
}

.unplayed-cards-text .suit-red {
  color: var(--light-red);
}

.dialog-status {
  margin: 0 0 14px;
  text-align: center;
}

.dialog-status:empty {
  display: none;
}

/* The charts scroll; the title and the close button stay in view. */
#stats[open] {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#statsContent {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 0 12px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.stats-chart {
  margin: 0 0 18px;
}

.stats-chart h3 {
  margin: 0 0 8px;
  font-size: 18px;
  text-align: center;
}

.stats-empty {
  margin: 0;
  padding: 8px 0;
  text-align: center;
}

.stats-chart-frame {
  position: relative;
  height: 220px;
}

.dialog-buttons {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 16px;
}

.dialog-cancel,
.dialog-confirm,
.dialog-half {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0;
  border: 0;
  background: none;
  font:
    700 18px Arial,
    sans-serif;
  cursor: pointer;
}

/* display: flex above would override the hidden attribute. */
.dialog-cancel[hidden],
.dialog-confirm[hidden],
.dialog-half[hidden] {
  display: none;
}

.dialog-cancel:disabled,
.dialog-confirm:disabled,
.dialog-half:disabled {
  opacity: 0.45;
  cursor: default;
}

.dialog-button-icon {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  flex: 0 0 52px;
  border-radius: 50%;
  font-size: 28px;
  line-height: 1;
  box-shadow: 0 2px 6px rgb(0 0 0 / 40%);
}

.dialog-cancel .dialog-button-icon {
  background: #e8352c;
}

.dialog-confirm .dialog-button-icon {
  background: #1414e6;
}

.dialog-half .dialog-button-icon {
  background: var(--dark-yellow);
}

/* Half-whist: its own row above Pass and Whist. */
.dialog-half-row {
  display: flex;
  justify-content: center;
  margin-top: 16px;
}

.dialog-cancel:focus-visible .dialog-button-icon,
.dialog-confirm:focus-visible .dialog-button-icon,
.dialog-half:focus-visible .dialog-button-icon {
  outline: 3px solid var(--felt-green);
  outline-offset: 2px;
}

.setup-limit {
  display: flex;
  justify-content: center;
  margin: 0 0 14px;
  padding: 6px 12px 12px;
  border: 1px solid rgb(107 214 107 / 60%);
  border-radius: 10px;
}

.setup-limit legend,
.stepper {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stepper button,
.bid-option {
  border: 1px solid #caf5ce;
  border-radius: 8px;
  background: rgb(49 92 60 / 70%);
  font:
    700 18px Arial,
    sans-serif;
  cursor: pointer;
}

.stepper button {
  width: 40px;
  height: 40px;
}

.stepper output {
  min-width: 48px;
  font-size: 26px;
  font-weight: 700;
  text-align: center;
}

.setup-rules {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 10px 12px 12px;
  border: 1px solid rgb(107 214 107 / 60%);
  border-radius: 10px;
}

.setup-rules label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  cursor: pointer;
}

.setup-rules input {
  width: 20px;
  height: 20px;
  accent-color: var(--felt-green);
}

/* The ‹ bid › stepper above misère; side by side in landscape. */
.bid-choice {
  display: grid;
  gap: 12px;
  align-items: center;
}

.bid-option {
  min-height: 40px;
  padding: 4px 2px;
  white-space: nowrap;
}

.bid-stepper {
  justify-content: center;
  /* Narrower on small phones, so the arrows stay inside the dialog. */
  gap: min(24px, 4vw);
}

.bid-stepper[hidden] {
  display: none;
}

.stepper button:disabled {
  opacity: 0.3;
  cursor: default;
}

/* Wide enough for "10×", so the arrows stay put while stepping. */
.bid-stepper-value {
  min-width: min(96px, 26vw);
  cursor: pointer;
}

.bid-stepper-value .suit-red {
  color: var(--light-red);
}

.bid-stepper-value--idle {
  opacity: 0.45;
}

/*
 * Landscape: two columns. The stepper beside misère; the game setup's rules
 * beside the game limit and Start; the pool beside the summary, last trick
 * and button. --dialog-area-height is the room above the hand (BaseDialog.ts).
 */
@media (orientation: landscape) {
  :is(#bidding, #contract) {
    width: min(560px, calc(100vw - 32px));
  }

  .bid-choice {
    grid-template-columns: 1fr 1fr;
  }

  .bid-choice > .bid-stepper[hidden] + .bid-option {
    grid-column: 1 / -1;
  }

  #gameSetup[open] {
    display: grid;
    grid-template:
      'rules limit' auto
      'rules .' 1fr
      'rules buttons' auto
      / 1fr 1fr;
    column-gap: 20px;
    width: min(640px, calc(100vw - 32px));
  }

  #gameSetup .setup-limit {
    grid-area: limit;
  }

  #gameSetup .setup-rules {
    grid-area: rules;
    align-self: start;
  }

  #gameSetup .dialog-buttons {
    grid-area: buttons;
    margin-top: 0;
  }

  /* The pool fills the height beside a 280px text column. */
  #scorecard[open] {
    /* The room minus the margin (8px), padding and border (40px) and the
       seats row (34px); at most what the margins (32px), padding and border
       (44px), gap (20px) and text column (280px) leave across. */
    --pool-height: min(
      calc(min(var(--dialog-area-height, 100dvh), 100dvh) - 82px),
      calc((100vw - 376px) * 3 / 4)
    );
    display: grid;
    grid-template:
      'pool summary' auto
      'pool trick' 1fr
      'pool buttons' auto
      'seats buttons' auto
      / calc(var(--pool-height) * 4 / 3) 280px;
    column-gap: 20px;
    width: fit-content;
  }

  #scorecard #scorecardPool {
    grid-area: pool;
    width: 100%;
    height: var(--pool-height);
    max-height: none;
  }

  #scorecardSummary {
    grid-area: summary;
  }

  #scorecard .scorecard-seats {
    grid-area: seats;
  }

  #scorecard .dialog-buttons {
    display: contents;
  }

  #scorecard .scorecard-last-trick {
    grid-area: trick;
  }

  #scorecard .dialog-confirm {
    grid-area: buttons;
    justify-self: end;
    align-self: end;
  }
}

.bid-option .suit-red {
  color: var(--light-red);
}

.bid-option:disabled {
  opacity: 0.3;
  cursor: default;
}

.bid-option[aria-pressed='true'] {
  border-color: var(--felt-green);
  background: var(--felt-green);
  box-shadow: 0 0 0 2px #fff inset;
}

.bid-option[aria-pressed='true'],
.bid-option[aria-pressed='true'] * {
  color: #12301a;
}

/* The pool: whists outside, the pool in the middle, the mountain inside. */
#scorecardPool {
  display: block;
  width: 100%;
  max-height: 50dvh;
}

.pool-line {
  fill: none;
  stroke: rgb(202 245 206 / 60%);
  stroke-width: 1;
}

.pool-limit-box {
  fill: #5d303a;
  stroke: var(--felt-green);
}

.pool-text {
  fill: #fff;
  font:
    13px Arial,
    sans-serif;
  text-anchor: middle;
}

.pool-limit {
  font-size: 16px;
  font-weight: 700;
}

.pool-fresh {
  font-weight: 700;
}

.pool-text.pool-pool {
  fill: var(--felt-green);
}

.pool-text.pool-mountain {
  fill: var(--light-red);
}

/* Names and balances under their sectors: left bot, you, right bot. */
.scorecard-seats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
  font-size: 17px;
}

.scorecard-seats > :first-child {
  text-align: left;
}

.scorecard-seats > :nth-child(2) {
  text-align: center;
}

.scorecard-seats > :last-child {
  text-align: right;
}

.scorecard-seat--local {
  font-weight: 700;
}

.scorecard-seats .score-positive {
  color: var(--felt-green);
}

.scorecard-seats .score-negative {
  color: var(--light-red);
}

/* Left of the button; above it in portrait. */
.scorecard-last-trick {
  font:
    700 17px Arial,
    sans-serif;
}

/* A long label wraps before the cards, never between them. */
.scorecard-last-trick-cards {
  white-space: nowrap;
}

/* Wide enough for the last trick and the button on one line. */
#scorecard {
  width: min(560px, calc(100vw - 32px));
}

.scorecard-last-trick .suit-red {
  color: var(--light-red);
}

@media (orientation: portrait) {
  /*
   * Decisions: one row per button on the right, the confirming one on top
   * (the bidding dialog has its stepper and misère above them).
   */
  :is(#bidding, #contract, #resumeGame, #whist, #halfWhist, #openCards)
    .dialog-buttons {
    flex-direction: column-reverse;
    align-items: flex-end;
    gap: 12px;
  }

  /* Pass ✖: the caption before the icon, so all icons line up. */
  :is(#bidding, #contract, #resumeGame, #whist, #halfWhist, #openCards)
    .dialog-cancel {
    flex-direction: row-reverse;
  }

  /* Half-whist between Whist and Pass. */
  #halfWhist[open] {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 12px;
  }

  #halfWhist :is(.dialog-half-row, .dialog-buttons) {
    display: contents;
  }

  #halfWhist .dialog-confirm {
    order: 1;
  }

  #halfWhist .dialog-half {
    order: 2;
  }

  #halfWhist .dialog-cancel {
    order: 3;
  }

  #scorecard .dialog-buttons {
    flex-wrap: wrap;
  }

  #scorecard .scorecard-last-trick {
    flex-basis: 100%;
    text-align: center;
  }

  #scorecard .scorecard-last-trick:empty {
    display: none;
  }

  #scorecard .dialog-confirm {
    margin-left: auto;
  }
}

/*
 * Grows across the top of the viewport while a bot thinks (botThinking.ts),
 * just below the notch or status bar and independent of the safe box.
 */
#thinkingBar {
  position: fixed;
  /* Capped at a sixth of the viewport, like safeAreaInsets.ts. */
  top: min(env(safe-area-inset-top, 0px), 16.667dvh);
  right: min(env(safe-area-inset-right, 0px), 16.667vw);
  left: min(env(safe-area-inset-left, 0px), 16.667vw);
  z-index: 1;
  height: 4px;
  background: var(--dark-yellow);
  transform: scaleX(var(--fraction, 0));
  transform-origin: left;
  pointer-events: none;
}

/* Its padding reports the safe-area insets to the HUD layout (safeAreaInsets.ts). */
#safeAreaProbe {
  position: fixed;
  top: 0;
  left: 0;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0)
    env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  visibility: hidden;
  pointer-events: none;
}
