@font-face {
  font-family: "Obcoro Pixel";
  src: url("../../assets/fusion-pixel-12px-monospaced-zh_tw.ttf.woff2") format("woff2");
  font-display: swap;
}

:root {
  --bg: #030909;
  --panel: #040b09f7;
  --ink: #70f995;
  --bright: #9affb1;
  --muted: #599d72;
  --line: #2a6940;
  --accent: #fa785c;
  --accent-line: #d45b43;
  color-scheme: dark;
  font: 16px/1.5 "Obcoro Pixel", monospace;
  font-synthesis: none;
  color: var(--ink);
  background: var(--bg);
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  --top-gutter: max(.5rem, env(safe-area-inset-top));
  --bottom-gutter: calc(.5rem + env(safe-area-inset-bottom));
  --play-height: calc(100dvh - var(--top-gutter) - var(--bottom-gutter));
  height: 100dvh;
  display: grid;
  place-items: end center;
  padding: var(--top-gutter) calc(.5rem + env(safe-area-inset-right))
           var(--bottom-gutter) calc(.5rem + env(safe-area-inset-left));
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
main { width: min(100%, 34rem); min-width: 0; }
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .625rem;
}
h1 { margin: 0; flex: none; color: var(--bright); font-size: 2.25rem; font-weight: 400; line-height: 1.2; }
.readout { display: grid; justify-items: end; line-height: 1.35; }
#level {
  display: inline-block;
  border-inline-start: 1px solid var(--line);
  padding-inline-start: .625rem;
  color: var(--bright);
  white-space: nowrap;
}
#status { min-height: 1.35em; margin: 0; color: var(--ink); text-align: right; }
main:has(#board[data-state="error"]) #status { color: var(--accent); }
.round-action {
  min-width: 44px;
  min-height: 44px;
  padding-inline: .75rem;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
footer {
  display: grid;
  gap: 4px;
  text-align: center;
  margin-top: clamp(.5rem, calc(20dvh - 8rem), 2.5rem);
  color: var(--muted);
  font-size: .75rem;
}

#board {
  display: grid;
  place-items: center;
  position: relative;
  isolation: isolate;
  grid-template-columns: minmax(0, 1fr);
  padding: .625rem .25rem .25rem;
  border: 1px solid var(--line);
  background: var(--panel);
  touch-action: none;
}
#sparks {
  position: absolute;
  inset: 0;
  z-index: 3;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#board[data-state="error"] { border-color: var(--accent-line); }
.bench { width: min(100%, max(20rem, calc(var(--play-height) - 16rem))); }
.rack, .buffer, #boxes, #tray, .box, #next, .slot {
  display: flex;
  align-items: center;
}
.rack, .buffer { justify-content: space-between; gap: .5rem; }
.rack { position: relative; margin-bottom: 1.5rem; }
#hint {
  position: absolute;
  top: 100%;
  left: 0;
  margin: 3px 0 0;
  color: var(--muted);
  font-size: .75rem;
  line-height: 1.35;
}
.cases { --case-gap: clamp(.375rem, 2vw, .875rem); position: relative; }
.label { color: var(--muted); font-size: .875rem; line-height: 1.4; }
.rack .label { margin-bottom: .375rem; }
.rack-caption {
  position: relative;
  display: flex;
  align-items: baseline;
  margin-bottom: .375rem;
}
.rack-caption .label { margin: 0; }
#combo {
  position: absolute;
  inset-inline-end: 0;
  bottom: 0;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding-inline: .25rem;
  color: var(--ink);
  font-size: .75rem;
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  transform-origin: right bottom;
}
#combo:empty { display: none; }
#combo::before {
  content: "";
  width: 2px;
  height: .6em;
  background: currentColor;
}
#combo[data-tier="2"] { color: var(--bright); background: #10251a; }
#combo[data-tier="2"]::before { box-shadow: -3px 0 currentColor; }
#combo[data-tier="3"] {
  padding-inline-start: .7rem;
  color: var(--bright);
  background: #163222;
  outline: 1px solid var(--line);
}
#combo[data-tier="3"]::before { box-shadow: -3px 0 currentColor, -6px 0 currentColor; }
#upcoming { text-align: right; }
#next { height: 2.25rem; justify-content: center; }
#boxes { gap: var(--case-gap); }
.box {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  gap: clamp(5px, 1.8vw, 10px);
  height: 2.25rem;
  padding: 0 clamp(6px, 2vw, 12px);
}
.box.closed { visibility: hidden; }
.slot {
  position: relative;
  width: 1.125rem;
  height: 1.125rem;
  justify-content: center;
}
canvas { image-rendering: pixelated; pointer-events: none; }
.case-shell, .tray-shell, .box-lid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.case-shell, .tray-shell { z-index: -1; }
.box-lid { z-index: 1; transform: scaleY(0); transform-origin: top center; }
.socket { width: 100%; height: 100%; }

#stack {
  margin-top: .5rem;
  display: grid;
  grid-template: repeat(var(--size), minmax(0, 1fr)) / repeat(var(--size), minmax(0, 1fr));
  position: relative;
  isolation: isolate;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 1;
  border: 1px solid var(--line);
}
.bench-floor {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.plank {
  display: grid;
  position: relative;
  grid-template: repeat(var(--h), 1fr) / repeat(var(--w), 1fr);
  pointer-events: none;
}
.board-grain {
  position: absolute;
  inset: 3px;
  width: calc(100% - 6px);
  height: calc(100% - 6px);
}
.mount { position: relative; display: grid; place-items: center; }
.bore { width: .75rem; height: .75rem; }
.release {
  position: absolute;
  inset: 0;
  width: clamp(36px, 10vw, 46px);
  height: auto;
  margin: auto;
  opacity: 0;
}
.mount.hit .release { animation: unseat 155ms ease-out; }
@keyframes unseat {
  0% { opacity: 1; transform: scale(.76); }
  30% { opacity: .9; }
  100% { opacity: 0; transform: scale(1.12); }
}
.screw {
  position: absolute;
  display: grid;
  place-items: center;
  appearance: none;
  /* Diagonal neighbors are at least sqrt(2) cells apart. This leaves room
     for the edge targets to shift inward while their circles stay disjoint. */
  width: clamp(100%, 44px, 120%);
  height: clamp(100%, 44px, 120%);
  clip-path: circle(50%);
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  pointer-events: auto;
  touch-action: none;
}
.screw[hidden] { display: none; }
.screw:disabled { pointer-events: none; }
/* Receipt text uses the same enamel swatches as the matching screws. */
.gain[data-color="0"] { --tone: #70f995; }
.gain[data-color="1"] { --tone: #fca447; }
.gain[data-color="2"] { --tone: #4b8581; }
.gain[data-color="3"] { --tone: #ffdb83; }
.gain[data-color="4"] { --tone: #ac80ac; }
.gain[data-color="5"] { --tone: #88c5ad; }
.fastener {
  display: block;
  flex: none;
  width: 1em;
  height: 1em;
  font-size: clamp(26px, 7.3vw, 35px);
}
.slot .fastener { font-size: 1.125rem; }
#next .fastener { font-size: 1.375rem; }
@media (hover: hover) {
  .screw:hover .fastener { outline: 1px solid var(--bright); }
}
.screw:active .fastener, .screw.queued .fastener { outline: 1px solid var(--bright); outline-offset: 1px; }
.screw.queued .fastener { animation: press 135ms ease-out; }
@keyframes press {
  0% { transform: scale(.78); }
  58% { transform: scale(1.09); }
  100% { transform: scale(1); }
}
.screw:focus-visible { outline: none; }
.screw:focus-visible .fastener { outline: 2px solid var(--bright); outline-offset: 2px; }

.buffer { border-top: 1px solid var(--line); padding-top: .375rem; }
#tray-count {
  margin-inline-start: .45em;
  color: var(--ink);
  font-size: .75rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.buffer[data-tight="true"] #tray-count { color: var(--accent); }
#tray { gap: 4px; }
#tray .slot {
  width: clamp(1.75rem, 7.2vw, 2.25rem);
  height: clamp(1.75rem, 7.2vw, 2.25rem);
  isolation: isolate;
}
#tray .fastener { font-size: 1.25rem; }
.slot.received .fastener { animation: seat 155ms ease-out; }
@keyframes seat {
  0% { transform: scale(1); }
  28% { transform: scale(1.17); }
  66% { transform: scale(.97); }
  100% { transform: scale(1); }
}
#flights { position: fixed; inset: 0; z-index: 10; overflow: hidden; pointer-events: none; }
.flying {
  position: absolute;
  inset: 0 auto auto 0;
  display: grid;
  place-items: center;
  margin: 0;
  line-height: 1;
  pointer-events: none;
  will-change: transform;
}
.flying[hidden] { display: none; }
.receipts {
  position: absolute;
  inset: 100% 0 auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--case-gap);
  pointer-events: none;
}
.gain {
  grid-row: 1;
  justify-self: center;
  margin-top: 2px;
  color: var(--tone, var(--bright));
  font-size: .875rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.gain:first-child { grid-column: 1; }
.gain:last-child { grid-column: 2; }
.gain[data-full="true"] { padding-inline: .25rem; border-inline: 1px solid currentColor; }
.gain[hidden] { display: none; }
@media (max-width: 35.99rem) and (orientation: portrait) {
  #board { margin-inline: -.5rem; padding-inline: 0; border-inline: 0; }
  #stack { border: 0; }
  .rack, .buffer { margin-inline: .5rem; }
}
@media (min-width: 36rem) and (min-height: 32.01rem) {
  body { align-items: center; }
  header { align-items: baseline; }
  .readout { display: flex; align-items: baseline; gap: 1rem; }
  #board { padding: 1rem; }
  .bench { width: min(100%, max(20rem, calc(var(--play-height) - 19rem))); }
  footer { margin-top: .75rem; }
}
@media (orientation: landscape) and (min-width: 34rem) and (max-height: 32rem) {
  body {
    --top-gutter: max(.25rem, env(safe-area-inset-top));
    --bottom-gutter: max(.25rem, env(safe-area-inset-bottom));
    align-items: center;
  }
  main {
    display: grid;
    width: min(100%, 48rem);
    grid-template-columns: minmax(0, 1fr) auto;
    gap: .25rem .75rem;
  }
  header { grid-area: 1 / 1; margin: 0; }
  h1 { font-size: 1.5rem; }
  .readout { display: flex; align-items: baseline; gap: .5rem; }
  footer { grid-area: 1 / 2; margin: 0; }
  #board { grid-column: 1 / -1; padding: .25rem; }
  .bench {
    --field: clamp(17rem, calc(var(--play-height) - 3rem), 26rem);
    width: 100%;
    display: grid;
    grid-template-columns: clamp(17rem, calc(100% - 14rem), var(--field)) minmax(0, 1fr);
    grid-template-rows: 1fr 1fr;
    gap: 1.5rem .75rem;
  }
  #stack { grid-area: 1 / 1 / 3 / 2; margin: 0; }
  .rack, .buffer { grid-column: 2; width: min(100%, 15rem); justify-self: center; }
  .rack { grid-row: 1; align-self: end; display: grid; justify-content: stretch; margin: 0; }
  .cases { order: 1; --case-gap: .375rem; }
  .box { gap: 3px; padding-inline: 4px; }
  #upcoming { display: flex; align-items: center; justify-content: space-between; }
  #upcoming .label { margin: 0; }
  #next { width: 2.25rem; }
  .buffer { grid-row: 2; align-self: start; }
}
@media (prefers-reduced-motion: reduce) {
  .screw.queued .fastener { animation: none; }
  .mount.hit .release { animation: hit-fade 100ms linear; }
  .slot.received .fastener { animation: seat-fade 100ms ease-out; }
}
@keyframes hit-fade { from { opacity: .75; } to { opacity: 0; } }
@keyframes seat-fade { from { opacity: .65; } to { opacity: 1; } }
