/* Chess play stage layout: player bars, board area and actions.
   Portrait: bars above and below the board. Wide screens: board on the left,
   bars and actions in a column on the right. */
.ch-stage {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-areas: "top" "board" "bottom" "actions";
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 0.5rem;
  padding: 8px max(24px, env(safe-area-inset-right, 0px))
    max(40px, calc(env(safe-area-inset-bottom, 0px) + 24px))
    max(24px, env(safe-area-inset-left, 0px));
}

.ch-bar-top {
  grid-area: top;
}

.ch-bar-bottom {
  grid-area: bottom;
}

.ch-board-area {
  grid-area: board;
  container-type: size;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.ch-actions {
  grid-area: actions;
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  gap: 0.5rem;
}

/* --- Player bars --- */
.ch-bar {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  min-height: 3.25rem;
  padding: 0.375rem 0.75rem;
  border: 2px solid transparent;
  border-radius: 0.875rem;
  background: rgba(240, 248, 242, 0.08);
  transition:
    background 0.2s,
    border-color 0.2s;
}

.ch-bar.is-active {
  border-color: var(--accent);
  background: rgba(232, 193, 90, 0.16);
}

.ch-bar.is-check {
  border-color: #e5534b;
  background: rgba(229, 83, 75, 0.18);
}

.ch-bar [data-bar-icon] {
  font-size: 2.2rem;
}

.ch-bar .ch-icon.is-black {
  filter: drop-shadow(0 0 1px rgba(244, 241, 232, 0.8));
}

.ch-bar-text {
  display: grid;
  min-width: 0;
  line-height: 1.2;
}

.ch-bar-text strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ch-bar-text small {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  min-height: 1.1rem;
  font-size: 0.75rem;
  font-weight: 700;
  opacity: 0.85;
}

.ch-bar-text small .ch-icon {
  margin-right: -0.5em;
  font-size: 1.7rem;
}

.ch-bar-status {
  margin-left: auto;
  font-size: 0.85rem;
  font-weight: 700;
  text-align: right;
}

.ch-bar.is-active .ch-bar-status {
  color: var(--accent);
}

.ch-bar.is-check .ch-bar-status {
  color: #ff8a80;
}

.ch-bar.is-thinking .ch-bar-status {
  animation: ch-pulse 1s ease-in-out infinite;
}

.ch-clock {
  flex: 0 0 auto;
  min-width: 4.4em;
  padding: 0.25rem 0.5rem;
  border-radius: 0.5rem;
  background: rgba(0, 0, 0, 0.35);
  font-size: 1.05rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  text-align: center;
  opacity: 0.7;
}

.ch-clock.is-running {
  background: #f4f1e8;
  color: #1d2621;
  opacity: 1;
}

.ch-clock.is-low {
  color: #ff8a80;
}

.ch-clock.is-running.is-low {
  background: #e5534b;
  color: #ffffff;
}

/* --- Actions --- */
.ch-action {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.125rem;
  min-height: 3.25rem;
  padding: 0.375rem 0.25rem;
  border: none;
  border-radius: 0.875rem;
  background: rgba(240, 248, 242, 0.1);
  color: inherit;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.ch-action:not(:disabled):hover {
  background: rgba(240, 248, 242, 0.18);
}

.ch-action:disabled {
  opacity: 0.35;
  cursor: default;
}

.ch-action svg {
  width: 1.375rem;
  height: 1.375rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (min-aspect-ratio: 6/5) {
  .ch-stage {
    grid-template-areas: "board top" "board actions" "board bottom";
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-columns: minmax(0, 1fr) minmax(14.375rem, 18.75rem);
  }

  .ch-bar {
    flex-wrap: wrap;
  }

  .ch-bar-status {
    order: 1;
    flex-basis: 100%;
    text-align: left;
  }

  .ch-clock {
    margin-left: auto;
  }

  .ch-bar-status:empty {
    display: none;
  }

  .ch-actions {
    grid-auto-flow: row;
    grid-auto-rows: max-content;
    align-content: center;
  }

  .ch-action {
    flex-direction: row;
    gap: 0.5rem;
    font-size: 0.95rem;
  }
}

@media (min-aspect-ratio: 6/5) and (max-height: 520px) {
  .ch-actions {
    grid-auto-flow: column;
    grid-auto-columns: minmax(0, 1fr);
  }

  .ch-action {
    flex-direction: column;
    gap: 0.125rem;
    font-size: 0.75rem;
  }
}

@keyframes ch-pulse {
  50% {
    opacity: 0.4;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ch-bar.is-thinking .ch-bar-status {
    animation: none;
  }
}
