*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  min-height: 100dvh;
  color: var(--ak-ink);
  font-family: var(--ak-font-body);
  font-size: 16px;
  font-weight: 600;
  line-height: 1.2;
  background-color: var(--ak-bg);
  background-image: var(--ak-bg-art);
  background-size: var(--ak-bg-size);
}

button {
  font-family: inherit;
  touch-action: manipulation;
}

img {
  display: block;
  max-width: 100%;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

[hidden] {
  display: none !important;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.play {
  --pad-top: max(6px, env(safe-area-inset-top, 0px));
  --pad-bot: max(8px, env(safe-area-inset-bottom, 0px));
  --pad-x: max(8px, env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px));
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(100%, 420px);
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 auto;
  padding: var(--pad-top) var(--pad-x) var(--pad-bot);
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  color: var(--ak-ink);
  background-color: var(--ak-bg);
  background-image: var(--ak-bg-art);
  background-size: var(--ak-bg-size);
}

.play.has-backdrop {
  background-image: var(--ak-backdrop-layer);
  background-size: cover;
  background-position: center top;
}

.mute {
  position: absolute;
  z-index: 8;
  top: var(--pad-top);
  right: max(4px, env(safe-area-inset-right, 0px));
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 2px solid var(--ak-outline);
  border-radius: var(--ak-radius);
  background: var(--ak-surface);
  color: var(--ak-ink);
  box-shadow: var(--ak-shadow);
  cursor: pointer;
}

.mute-icon {
  width: 22px;
  height: 22px;
  pointer-events: none;
}

.mute.is-muted .wave {
  display: none;
}

.mute.is-muted::after {
  content: "";
  position: absolute;
  width: 22px;
  height: 3px;
  background: var(--ak-accent);
  transform: rotate(-35deg);
}

.mute:focus-visible,
.card:focus-visible,
.plaque:focus-visible,
.btn-go:focus-visible,
.btn-alt:focus-visible {
  outline: 3px solid var(--ak-focus);
  outline-offset: 2px;
}

.top,
.voice,
.goal,
.actions,
.end,
.rail,
.true-rule,
.spare-tray,
.plaques {
  position: relative;
  z-index: 5;
  flex: 0 0 auto;
}

.top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding-right: 52px;
}

.play-logo {
  width: auto;
  max-width: 46%;
  height: auto;
  max-height: 44px;
  object-fit: contain;
  pointer-events: none;
}

.turn {
  min-width: 0;
}

.turn-who {
  margin: 0;
  font-family: var(--ak-font-display);
  font-size: 22px;
  font-weight: 400;
  line-height: 1;
}

.turn-sub {
  margin: 2px 0 0;
  color: var(--ak-ink-muted);
  font-size: 14px;
}

.voice,
.goal,
.why,
.result-card,
.spare-tray {
  background: var(--ak-surface);
  border: 2px solid var(--ak-outline);
  border-radius: var(--ak-radius);
  box-shadow: var(--ak-shadow);
}

.voice,
.goal {
  margin: 0;
  padding: 6px 10px;
}

.voice {
  color: var(--ak-ink);
  font-size: 15px;
  line-height: 1.25;
}

.voice.is-invite {
  color: var(--ak-accent);
  font-weight: 700;
}

.goal {
  color: var(--ak-ink);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.25;
}

.stage {
  position: relative;
  z-index: 1;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.spare-tray {
  padding: 4px 4px 5px;
}

.spare-label {
  margin: 0 2px 3px;
  color: var(--ak-ink-muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.2;
  text-align: center;
}

.spares {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 4px;
  min-height: 48px;
}

.board {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  container-type: size;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-frame {
  width: min(100cqi, calc(100cqh * 384 / 330));
  aspect-ratio: 384 / 330;
  max-height: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 7%;
  padding: 22% 6.5% 6%;
  background-image: url("./art/column-board.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
}

.col {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, 1fr);
  align-content: stretch;
  gap: 3px;
}

.col.is-crowd {
  grid-template-columns: 1fr 1fr;
  gap: 3px;
}

.card {
  --tilt: 0deg;
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ak-accent);
  cursor: pointer;
}

.spares .card {
  min-height: 48px;
  height: 48px;
}

.card img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
}

.card-text {
  position: relative;
  z-index: 1;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  max-width: 84%;
  margin-top: 6%;
  color: var(--ak-accent);
  font-family: var(--ak-font-body);
  font-size: clamp(12px, 3.2vw, 18px);
  font-weight: 700;
  line-height: 1.05;
  text-align: center;
  overflow-wrap: anywhere;
}

.spares .card-text {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.05;
}

.col.is-crowd .card-text {
  font-size: 11px;
  max-width: 92%;
}

.stamp {
  position: absolute;
  z-index: 2;
  right: 10%;
  bottom: 12%;
  width: min(14px, 24%);
  aspect-ratio: 1;
  border: 2px solid var(--ak-accent-2);
  border-radius: 50%;
  background: var(--ak-accent);
  box-shadow: 0 1px 0 var(--ak-outline);
  opacity: 0;
  transform: scale(0.5);
}

.card.is-stamped .stamp {
  opacity: 1;
  transform: scale(1);
}

.tick {
  position: absolute;
  z-index: 3;
  inset: 18% 22%;
  border-left: 4px solid var(--ak-accent);
  border-bottom: 4px solid var(--ak-accent);
  opacity: 0;
  transform: rotate(-45deg) scale(0.4);
  pointer-events: none;
}

.card.is-placed {
  pointer-events: none;
  cursor: default;
}

.card.is-out {
  filter: saturate(0.72) brightness(0.9);
}

.card.is-break {
  outline: 3px solid var(--ak-accent);
  outline-offset: 1px;
  z-index: 2;
}

.true-rule {
  display: flex;
  align-items: center;
  min-height: 48px;
  margin: 0;
  padding: 6px 10px 6px 36%;
  overflow: hidden;
  color: var(--ak-accent);
  font-family: var(--ak-font-body);
  font-size: 15px;
  font-weight: 700;
  line-height: 1.15;
}

.true-rule img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

.true-rule span {
  position: relative;
  z-index: 1;
}

.rail {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
}

.rail-track {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 28px;
  padding: 0 8px;
  border: 2px solid var(--ak-outline);
  border-radius: 999px;
  background: var(--ak-accent-2);
  box-shadow: var(--ak-shadow);
}

.notch {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ak-ink);
  opacity: 0.35;
}

.bead {
  --notch: 0;
  position: absolute;
  top: 2px;
  left: calc(2px + (var(--notch) * (100% - 26px)) / 6);
  width: 20px;
  height: 20px;
  border: 2px solid var(--ak-outline);
  border-radius: 50%;
  background: var(--ak-accent);
  box-shadow: inset 0 0 0 3px var(--ak-accent-2);
}

.bead.is-miss::after {
  content: "";
  position: absolute;
  left: 1px;
  right: 1px;
  top: 7px;
  height: 2px;
  background: var(--ak-ink);
  transform: rotate(-32deg);
}

.rail-score {
  flex: 0 0 auto;
  min-width: 76px;
  margin: 0;
  color: var(--ak-accent);
  font-family: var(--ak-font-display);
  font-size: 22px;
  font-weight: 400;
  line-height: 1;
  text-align: right;
}

.plaques {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  perspective: 700px;
}

.plaque {
  position: relative;
  display: flex;
  align-items: center;
  min-width: 48px;
  min-height: 58px;
  padding: 4px 6px 4px 44%;
  border: 0;
  background: transparent;
  color: var(--ak-ink);
  text-align: left;
  cursor: pointer;
  overflow: hidden;
}

.plaque img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 50%;
}

.plaque-text {
  position: relative;
  z-index: 1;
  font-family: var(--ak-font-body);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.12;
}

.crack {
  position: absolute;
  z-index: 2;
  left: 7%;
  top: 20%;
  width: 18%;
  height: 58%;
  opacity: 0;
  pointer-events: none;
  background:
    linear-gradient(var(--ak-ink), var(--ak-ink)) left 35% top 0 / 2px 46% no-repeat,
    linear-gradient(var(--ak-ink), var(--ak-ink)) left 62% top 34% / 2px 62% no-repeat;
}

.plaque.is-crack .crack {
  opacity: 1;
}

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

.btn-go,
.btn-alt {
  min-height: 48px;
  padding: 8px 12px;
  border: 2px solid var(--ak-outline);
  border-radius: var(--ak-radius);
  box-shadow: var(--ak-shadow);
  font-family: var(--ak-font-display);
  font-size: 20px;
  font-weight: 400;
  line-height: 1;
  cursor: pointer;
}

.btn-go {
  flex: 1 1 auto;
  background: var(--ak-btn-fill);
  color: var(--ak-btn-ink);
}

.btn-alt {
  flex: 0 0 auto;
  min-width: 72px;
  background: var(--ak-surface);
  color: var(--ak-choice-ink);
  font-size: 18px;
}

.btn-go:active {
  background: var(--ak-btn-active);
}

.btn-alt:active {
  background: var(--ak-choice-bg);
}

.end {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.why {
  margin: 0;
  padding: 8px 10px;
  color: var(--ak-ink);
  font-family: var(--ak-font-display);
  font-size: 18px;
  font-weight: 400;
  line-height: 1.2;
}

.result-card {
  padding: 8px;
  border-color: var(--ak-accent);
}

.result-says {
  margin: 0 0 8px;
  color: var(--ak-ink);
  font-family: var(--ak-font-display);
  font-size: 20px;
  font-weight: 400;
  line-height: 1.15;
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.result-actions .btn-go,
.result-actions .btn-alt {
  width: 100%;
}

.play.is-ended .voice,
.play.is-ended .goal,
.play.is-ended .actions,
.play.is-ended .spare-tray {
  display: none;
}

.play.is-result .plaques {
  display: none;
}

.fx {
  position: absolute;
  z-index: 4;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.fx i {
  position: absolute;
  left: 50%;
  top: 42%;
  width: 8px;
  height: 8px;
  margin: 0;
  border-radius: 2px;
  background: var(--ak-accent);
  opacity: 0;
}

.fx i:nth-child(odd) {
  background: var(--ak-accent-2);
  border-radius: 50%;
}

.fx i:nth-child(3n) {
  width: 11px;
  height: 11px;
}

@media (min-width: 520px) {
  .play {
    border-left: 2px solid var(--ak-outline);
    border-right: 2px solid var(--ak-outline);
    box-shadow: var(--ak-shadow);
  }
}

@media (prefers-reduced-motion: reduce) {
  .plaque.is-crack {
    transform: translateY(16px) rotate(2deg);
    opacity: 0.6;
  }

  .plaque.is-flat {
    transform: scaleY(0.4);
    opacity: 0.45;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .mute:active,
  .btn-go:active,
  .btn-alt:active {
    transform: scale(0.96, 0.92);
  }

  .card:active:not(.is-placed):not(.is-sliding) {
    transform: rotate(var(--tilt)) scale(0.96, 0.92);
  }

  .plaque:active:not(.is-crack):not(.is-flat) {
    transform: scale(0.97, 0.92);
  }

  .play-logo {
    animation: logo-idle 3.6s ease-in-out infinite;
  }

  .board-frame:not(.is-point):not(.is-shake) {
    animation: board-idle 5.4s ease-in-out infinite;
  }

  .play.is-win .board-frame {
    animation: chase 1.05s linear 3;
  }

  .spares .card:not(.is-sliding):not(.is-coach) {
    animation: bob 3.2s ease-in-out infinite;
    animation-delay: calc(var(--i, 0) * 0.14s);
  }

  .col .card:not(.is-sliding) {
    transform: rotate(var(--tilt));
  }

  .card.is-coach {
    animation: coach 1.05s ease-in-out infinite;
  }

  .card.is-sliding {
    animation: none;
  }

  .card.is-yes .tick {
    animation: tickpop 680ms cubic-bezier(0.34, 1.56, 0.64, 1);
  }

  .card.is-no {
    animation: shake 420ms ease-in-out;
  }

  .stamp {
    transition: transform 280ms cubic-bezier(0.34, 1.56, 0.64, 1), opacity 160ms linear;
  }

  .bead {
    transition: left 420ms cubic-bezier(0.34, 1.56, 0.64, 1);
  }

  .bead.is-drop {
    animation: bead-squash 420ms cubic-bezier(0.34, 1.56, 0.64, 1);
  }

  .turn.is-cue .turn-who {
    animation: cuepop 420ms cubic-bezier(0.34, 1.45, 0.64, 1);
  }

  .board-frame.is-point {
    animation: point 1.15s ease-in-out infinite;
  }

  .btn-alt.is-point {
    animation: point 1.15s ease-in-out infinite;
  }

  .board-frame.is-shake {
    animation: shake 420ms ease-in-out;
  }

  .plaques.is-rise .plaque {
    animation: rise 460ms cubic-bezier(0.34, 1.45, 0.64, 1) both;
    animation-delay: calc(var(--i, 0) * 70ms);
  }

  .plaque.is-right {
    box-shadow: var(--ak-shadow), 0 0 0 3px var(--ak-accent-2), 0 0 14px var(--ak-glow);
    animation: gleam 900ms ease-in-out 3;
  }

  .plaque.is-right::after {
    content: "";
    position: absolute;
    z-index: 2;
    inset: 22% 6%;
    background: linear-gradient(105deg, transparent 30%, var(--ak-surface) 50%, transparent 70%);
    opacity: 0;
    pointer-events: none;
    animation: sweep 900ms ease-in-out 2;
  }

  .plaque.is-flat {
    animation: flatten 480ms ease-in forwards;
  }

  .plaque.is-crack {
    animation: sink 620ms cubic-bezier(0.22, 1, 0.36, 1) forwards;
  }

  .true-rule.is-writing span {
    animation: write 720ms steps(16, end) both;
  }

  .fx.is-on i {
    animation: burst 900ms cubic-bezier(0.16, 0.84, 0.32, 1) both;
  }

  @keyframes bob {
    0%,
    100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
  }

  @keyframes coach {
    0%,
    100% { transform: translateY(0) scale(1); }
    50% { transform: translateY(-5px) scale(1.05, 0.94); }
  }

  @keyframes logo-idle {
    0%,
    100% { transform: translateY(0) rotate(0deg); }
    50% { transform: translateY(-2px) rotate(-0.6deg); }
  }

  @keyframes board-idle {
    0%,
    100% { transform: translateY(0); }
    50% { transform: translateY(-3px); }
  }

  @keyframes cuepop {
    0% { transform: scale(1); }
    40% { transform: scale(1.08, 0.9); }
    100% { transform: scale(1); }
  }

  @keyframes point {
    50% { box-shadow: var(--ak-shadow), 0 0 0 4px var(--ak-accent); }
  }

  @keyframes shake {
    0%,
    100% { transform: translateX(0) rotate(var(--tilt, 0deg)); }
    25% { transform: translateX(-6px) rotate(var(--tilt, 0deg)); }
    75% { transform: translateX(6px) rotate(var(--tilt, 0deg)); }
  }

  @keyframes tickpop {
    0% { opacity: 0; transform: rotate(-45deg) scale(0.3); }
    35% { opacity: 1; transform: rotate(-45deg) scale(1.15); }
    100% { opacity: 0; transform: rotate(-45deg) scale(1); }
  }

  @keyframes bead-squash {
    0% { transform: scale(1.15, 0.82); }
    45% { transform: scale(0.9, 1.16); }
    100% { transform: scale(1); }
  }

  @keyframes rise {
    0% { opacity: 0; transform: translateY(24px) scale(0.94, 1.08); }
    100% { opacity: 1; transform: translateY(0) scale(1); }
  }

  @keyframes gleam {
    0%,
    100% { box-shadow: var(--ak-shadow), 0 0 0 2px var(--ak-accent-2), 0 0 0 var(--ak-glow); }
    50% { box-shadow: var(--ak-shadow), 0 0 0 4px var(--ak-accent), 0 0 16px var(--ak-glow); }
  }

  @keyframes sweep {
    0% { opacity: 0; transform: translateX(-40%); }
    40% { opacity: 0.55; }
    100% { opacity: 0; transform: translateX(40%); }
  }

  @keyframes flatten {
    0% { transform: rotateX(0deg) scaleY(1); opacity: 1; }
    100% { transform: rotateX(68deg) scaleY(0.32); opacity: 0.42; }
  }

  @keyframes sink {
    0% { transform: translateY(0) scale(1); opacity: 1; }
    35% { transform: translateY(-4px) scale(1.05, 0.9); }
    100% { transform: translateY(18px) rotate(3deg); opacity: 0.5; }
  }

  @keyframes chase {
    0% { box-shadow: 0 0 0 3px var(--ak-accent), var(--ak-shadow); }
    33% { box-shadow: 0 0 0 3px var(--ak-accent-2), 0 0 12px var(--ak-glow); }
    66% { box-shadow: 0 0 0 3px var(--ak-accent), var(--ak-shadow); }
    100% { box-shadow: 0 0 0 3px var(--ak-accent-2), 0 0 16px var(--ak-glow); }
  }

  @keyframes write {
    from { clip-path: inset(0 100% 0 0); }
    to { clip-path: inset(0 0 0 0); }
  }

  @keyframes burst {
    0% { opacity: 1; transform: translate(-50%, -50%) scale(0.4) rotate(0deg); }
    100% {
      opacity: 0;
      transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) rotate(var(--rot)) scale(1);
    }
  }
}
