/*
 * Reversi over the city.
 *
 * The panel is a sibling of the scene and the chrome, fixed over both. It does not
 * stop the renderer, and the full-screen scrim is deliberately not
 * backdrop-filtered: blurring a constantly repainting 384x216 canvas is expensive
 * on phones, and the city staying crisp around the card is the point of putting
 * the board over the picture.
 *
 * The board is a CSS grid of real buttons. The browser does the hit testing, the
 * keyboard reaches every legal cell, and the screen reader gets a label per cell;
 * a canvas board would have to reimplement all three.
 */

#reversi {
  /* The felt and the two disc materials. The dark disc needs a visible rim: on a
     dark board a pure-black circle loses its silhouette. */
  --rev-felt-1: #34422f;
  --rev-felt-2: #243026;
  --rev-grid: rgba(239, 230, 218, 0.1);
  --rev-disc-black: radial-gradient(circle at 35% 30%, #3a2b20, #1a1310 62%);
  --rev-disc-white: radial-gradient(circle at 35% 30%, #fff8ec, #e6d7bf 62%, #cfbda2);

  position: fixed;
  inset: 0;
  z-index: 35; /* over chrome and popovers, under the audio-stall modal (40) */
  display: grid;
  place-items: center;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

/* The author display:grid would otherwise beat the user agent's [hidden] rule. */
#reversi[hidden] {
  display: none;
}

.reversi-scrim {
  position: absolute;
  inset: 0;
  background: radial-gradient(
    ellipse at center,
    rgba(10, 8, 7, 0.3),
    rgba(5, 6, 13, 0.72)
  );
}

.reversi [hidden] {
  display: none;
}

.reversi-card {
  position: relative; /* above the scrim */
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(92vw, 520px);
  max-width: 100%;
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--glass-solid);
  box-shadow: 0 24px 60px var(--shadow-strong);
  user-select: none;
}

.reversi-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.reversi-heading h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.reversi-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}

/* The header's close button uses the shared icon-btn, but the card is a denser
   surface than the topbar; pull it up to the title's line. */
#reversi-close {
  margin: -4px -4px 0 0;
}

/* The card is a scrolling box, so the shared tooltip's upward placement would be
   clipped. Inside it, open downward from the button. */
.reversi-card [data-tip]::after {
  top: calc(100% + 9px);
  bottom: auto;
  left: auto;
  right: 0;
  transform: none;
}

.reversi-score {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  font-size: 13px;
}

.reversi-side {
  display: flex;
  align-items: center;
  gap: 7px;
  flex: 1 1 0;
  min-width: 0;
}

.reversi-side:first-child {
  justify-content: flex-end;
}

.reversi-side:last-child {
  justify-content: flex-start;
}

.reversi-name {
  color: var(--text-dim);
  white-space: nowrap;
}

.reversi-count {
  min-width: 1.6ch;
  color: var(--text);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.reversi-vs {
  color: var(--text-dim);
}

.reversi-disc {
  flex: none;
  width: 14px;
  aspect-ratio: 1;
  border-radius: 50%;
}

.reversi-disc[data-color="black"] {
  background: var(--rev-disc-black);
  box-shadow: inset 0 0 0 1px rgba(239, 230, 218, 0.32);
}

.reversi-disc[data-color="white"] {
  background: var(--rev-disc-white);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.reversi-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 2.7em;
}

.reversi-status {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  font-size: 13px;
  line-height: 1.35;
  color: var(--text);
}

.reversi-status.is-dim {
  color: var(--text-dim);
}

.reversi-status.is-win {
  color: var(--accent);
  font-weight: 600;
}

.reversi-status.is-lose {
  color: var(--danger);
  font-weight: 600;
}

/* Per-move timeout bars: opponent above the board, visitor below it. The active
   side drains from full to empty; the waiting side stays full. */
.reversi-time {
  flex: none;
  height: 5px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
}

.reversi-time.is-opp {
  margin: 1px 0 2px;
}

.reversi-time.is-you {
  margin: 2px 0 1px;
}

.reversi-time i {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--accent);
  transform-origin: left center;
  transition: transform 0.25s linear, background 0.2s linear;
}

.reversi-time.is-opp i {
  background: var(--text-dim);
}

.reversi-time.is-low i {
  background: var(--danger);
}

.reversi-time[hidden] {
  display: none;
}

/* The field. 54dvh leaves room for the header, score, clock and buttons on a
   desktop; on a phone width is the limiting dimension and the board simply fills
   the card. Cells carry their own 1px grid line, so no gap eats a touch target. */
.reversi-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(100%, 54dvh, 460px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 3px;
  overflow: hidden;
  border: 1px solid rgba(232, 184, 122, 0.28);
  border-radius: 8px;
  background: linear-gradient(145deg, var(--rev-felt-1), var(--rev-felt-2));
  box-shadow: inset 0 2px 14px rgba(0, 0, 0, 0.35);
  touch-action: manipulation;
}

.reversi-board:focus {
  outline: 2px solid var(--accent-dim);
  outline-offset: 4px;
}

.reversi-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 2px;
  background: transparent;
  color: inherit;
  box-shadow: inset 0 0 0 1px var(--rev-grid);
  cursor: default;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.reversi-cell:disabled {
  opacity: 1; /* the disc and the grid must keep their colours */
}

.reversi-cell.is-legal {
  cursor: pointer;
}

/* The click is in flight: the board is disabled until the server's state arrives,
   and this tint says the cell was heard rather than ignored. */
.reversi-cell.is-pending {
  background: rgba(232, 184, 122, 0.14);
}

.reversi-cell.is-legal::after {
  content: "";
  position: absolute;
  width: 26%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.6;
  transition: opacity 0.12s, transform 0.12s;
}

.reversi-cell.is-legal:hover {
  background: rgba(232, 184, 122, 0.08);
}

.reversi-cell.is-legal:hover::after,
.reversi-cell.is-legal:focus-visible::after {
  opacity: 1;
  transform: scale(1.18);
}

.reversi-cell:focus-visible {
  z-index: 1;
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.reversi-cell.is-last {
  box-shadow: inset 0 0 0 2px rgba(232, 184, 122, 0.5), inset 0 0 0 1px var(--rev-grid);
}

.reversi-disc-piece {
  display: block;
  width: 78%;
  aspect-ratio: 1;
  border-radius: 50%;
  pointer-events: none; /* the button is the target, not this span */
  transform-origin: center;
  opacity: 0;
}

.reversi-disc-piece[data-color="black"] {
  background: var(--rev-disc-black);
  box-shadow: inset 0 0 0 1px rgba(239, 230, 218, 0.32),
    inset 0 2px 2px rgba(255, 255, 255, 0.06), 0 2px 3px rgba(0, 0, 0, 0.45);
  opacity: 1;
}

.reversi-disc-piece[data-color="white"] {
  background: var(--rev-disc-white);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18),
    inset 0 2px 2px rgba(255, 255, 255, 0.45), 0 2px 3px rgba(0, 0, 0, 0.4);
  opacity: 1;
}

.reversi-disc-piece.is-new {
  animation: reversi-place 0.38s cubic-bezier(0.4, 0, 0.25, 1);
}

.reversi-disc-piece.is-flip {
  animation: reversi-flip 0.34s ease-in-out;
}

/* The placed disc drops in from above and slightly to the right, with a small
   settle at the end. The flips start with it and finish at the same moment. */
@keyframes reversi-place {
  0% {
    opacity: 0;
    transform: translate(18%, -52%) scale(0.78) rotate(8deg);
  }
  25% {
    opacity: 1;
  }
  62% {
    transform: translate(2%, -4%) scale(1.03) rotate(-2deg);
  }
  82% {
    transform: translate(0, 0) scale(0.98) rotate(0deg);
  }
  100% {
    opacity: 1;
    transform: translate(0, 0) scale(1) rotate(0deg);
  }
}

/* The colour is already the new one when this starts; at 90 degrees the disc is
   edge-on, so the switch happens where it cannot be seen. */
@keyframes reversi-flip {
  0% {
    transform: rotateY(0deg);
  }
  50% {
    transform: rotateY(90deg);
  }
  100% {
    transform: rotateY(0deg);
  }
}

.reversi-actions {
  display: flex;
  gap: 8px;
}

.reversi-btn {
  flex: 1 1 auto;
  min-height: 44px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card-2);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s;
}

.reversi-btn:hover {
  background: var(--card);
  border-color: var(--accent-dim);
}

.reversi-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.reversi-btn.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}

.reversi-btn.primary:hover {
  border-color: var(--accent-bright);
  background: var(--accent-bright);
}

.reversi-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.reversi-note {
  margin: 0;
  font-size: 11px;
  line-height: 1.4;
  color: var(--text-dim);
  text-align: center;
}

@media (max-width: 720px) {
  .reversi-card {
    width: calc(100vw - 16px);
    gap: 8px;
    padding: 12px;
  }

  .reversi-heading h2 {
    font-size: 18px;
  }

  .reversi-sub {
    display: none;
  }

  .reversi-score {
    padding: 5px 8px;
    font-size: 12px;
  }

  .reversi-board {
    padding: 2px;
  }
}

/* A landscape phone has almost no vertical room. The board is already capped by
   dvh; the rest of the card gives up its breathing space so the whole game still
   fits without the page scrolling. */
@media (max-height: 620px) {
  .reversi-card {
    gap: 6px;
    padding: 8px 12px;
  }

  .reversi-heading h2 {
    font-size: 16px;
  }

  .reversi-sub,
  .reversi-note {
    display: none;
  }

  .reversi-score {
    padding: 4px 8px;
  }

  .reversi-status {
    font-size: 12px;
  }

  .reversi-status-row {
    min-height: 0;
  }

  .reversi-actions .reversi-btn {
    min-height: 40px;
    padding: 6px 12px;
  }
}

/* A landscape phone is wide but not tall. Capping the card at the board's own
   height keeps it from becoming a letterbox with a small board floating in the
   middle; the city gets the sides back. Portrait phones are unaffected: there the
   width is the limiting dimension and the card should use it. */
@media (max-height: 620px) and (orientation: landscape) {
  .reversi-card {
    width: min(92vw, calc(54dvh + 56px));
  }
}

@media (hover: none) {
  /* The cells are the buttons; a slightly larger dot is easier to see under a
     finger, and there is no hover state to enlarge it. */
  .reversi-cell.is-legal::after {
    width: 30%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .reversi-disc-piece.is-new,
  .reversi-disc-piece.is-flip {
    animation: none !important;
  }

  .reversi-time i {
    transition: none;
  }
}
