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

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

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

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  --viewport: 100vh;
  --top: max(.5rem, env(safe-area-inset-top));
  --bottom: calc(.5rem + env(safe-area-inset-bottom));
  --side: clamp(0px, calc(50vw - 180px), 6px);
  --frame-pad: clamp(0px, calc(50vw - 177px), 3px);
  --gap: clamp(6px, 1vh, 10px);
  --tray-height: clamp(44px, 8.5vh, 76px);
  /* Allow for the title, status, score strip and footer before sizing the square. */
  --grid-limit: min(31rem, max(0px, calc(var(--viewport) - var(--top) - var(--bottom)
    - var(--tray-height) - 2 * var(--gap) - 2 * var(--frame-pad) - 9rem)));
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  justify-items: center;
  height: var(--viewport);
  padding: var(--top) max(var(--side), env(safe-area-inset-right))
           var(--bottom) max(var(--side), env(safe-area-inset-left));
  -webkit-tap-highlight-color: transparent;
}

@supports (height: 100dvh) {
  body { --viewport: 100dvh; }
}

main {
  display: grid;
  /* Keep the tray within thumb reach. */
  grid-template-rows: minmax(0, 3fr) auto auto minmax(.375rem, 1fr) auto;
  width: min(100%, calc(var(--grid-limit) + var(--frame-pad) * 2 + 2px));
  min-width: 0;
}

header {
  grid-row: 2;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: .375rem;
  margin-bottom: .625rem;
}

h1 {
  margin: 0;
  color: var(--bright);
  font-size: clamp(1.75rem, 5vw, 2.25rem);
  font-weight: 400;
  letter-spacing: .04em;
  line-height: 1.2;
  white-space: nowrap;
}

.readout {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: baseline;
  gap: .125rem .5rem;
  min-width: 0;
  text-align: right;
  font-size: .875rem;
  line-height: 1.35;
  font-variant-numeric: tabular-nums;
}

#level { display: inline-block; color: var(--ink); transform-origin: right center; }
#goal { color: var(--bright); }
#level, #goal { white-space: nowrap; }

#status {
  flex-basis: 100%;
  min-height: 1.25rem;
  margin: 0;
  color: var(--muted);
  font-size: .75rem;
}

#status[data-tone="error"] { color: var(--accent); font-size: .875rem; }

button {
  appearance: none;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  touch-action: manipulation;
}

button:focus-visible { outline: 2px solid var(--bright); outline-offset: 2px; }

#board {
  grid-row: 3;
  display: grid;
  gap: var(--gap);
  padding: var(--frame-pad);
  border: 1px solid var(--line);
  background: var(--panel);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.board-strip {
  display: flex;
  align-items: center;
  gap: clamp(.625rem, 2.4vw, 1rem);
  min-height: 1.5rem;
  font-size: .875rem;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

#score { color: var(--bright); }
#score, #combo { white-space: nowrap; }
#combo { position: relative; color: var(--ink); transform-origin: right center; }
#combo[data-rank="0"] { color: var(--muted); }
#combo[data-rank="1"] { --ticks: 5px; }
#combo[data-rank="2"] { --ticks: 12px; }
#combo[data-rank="3"] { --ticks: 19px; color: var(--bright); }
#combo::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: -3px;
  width: var(--ticks, 0px);
  height: 2px;
  background: repeating-linear-gradient(to left, currentColor 0 5px, transparent 5px 7px);
}

.progress-track {
  flex: 1;
  height: 4px;
  overflow: hidden;
  background: #1a3826;
}

#progress {
  height: 100%;
  background: var(--ink);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 165ms ease-out;
}

.grid-stage {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  min-width: 0;
  aspect-ratio: 1;
  overflow: hidden;
  isolation: isolate;
  outline: 1px solid var(--line);
  background: var(--bg);
}

#grid, #effects {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#grid {
  display: grid;
  grid-template: repeat(8, minmax(0, 1fr)) / repeat(8, minmax(0, 1fr));
  background: #09140e;
  touch-action: none;
}

#effects { z-index: 1; display: block; pointer-events: none; }

.cell {
  --divider-x: #426d4e;
  --divider-y: #426d4e;
  position: relative;
  cursor: pointer;
  touch-action: none;
  box-shadow: inset -1px 0 var(--divider-x), inset 0 -1px var(--divider-y);
}

/* Keep the grid's eight pitches identical for pointer and canvas geometry. */
.cell:nth-child(8n + 4) { --divider-x: #598361; }
.cell:nth-child(n + 25):nth-child(-n + 32) { --divider-y: #598361; }
.cell:focus-visible { z-index: 1; outline-offset: -2px; }

#tray {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(.375rem, 1.8vw, .625rem);
}

.piece {
  --unit: clamp(8px, min(3.4vw, 1.5vh), 14px);
  position: relative;
  display: grid;
  place-items: center;
  min-width: 44px;
  height: var(--tray-height);
  border: 1px solid #467957;
  background: #0a1510;
  cursor: grab;
  touch-action: none;
  transition: transform 175ms cubic-bezier(.18,.96,.28,1.6);
}

.piece[aria-pressed="true"] {
  border-color: var(--accent);
  background: #191711;
}

.piece[aria-pressed="true"]::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 7px;
  height: 7px;
  background: var(--accent);
  clip-path: polygon(0 0, 100% 0, 0 100%);
}

.piece:enabled:is(:active, [data-held]) {
  transform: translateY(1px) scale(.94);
  background: #312018;
  cursor: grabbing;
  transition: transform 42ms cubic-bezier(.15,.8,.4,1);
}

.piece:disabled { border-color: #203d2a; background: #07100b; cursor: default; }
.piece:disabled:empty::after {
  content: "";
  width: 12px;
  height: 1px;
  background: #36553f;
}

.piece-image {
  display: block;
  width: calc(var(--w) * var(--unit));
  height: calc(var(--h) * var(--unit));
  image-rendering: pixelated;
  pointer-events: none;
}

#tray .piece-image { animation: deal-in 160ms cubic-bezier(.18,.74,.25,1) both; }
@keyframes deal-in {
  from { opacity: .3; transform: translateY(5px) scale(.94); }
  to { opacity: 1; transform: none; }
}

.drag-piece {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  pointer-events: none;
  transform-origin: top left;
  opacity: .94;
}

.drag-piece[hidden] { display: none; }

@media (hover: hover) and (pointer: fine) {
  .piece:enabled:not([aria-pressed="true"]):hover { background: #11271b; border-color: var(--ink); }
  .cell[data-tone="0"]:hover { background: #12271a; }
}

#resume-round {
  min-width: 44px;
  min-height: 44px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--bright);
  font: inherit;
  cursor: pointer;
}

footer {
  grid-row: 5;
  align-self: center;
  padding-inline-start: .25rem;
  color: var(--muted);
  font-size: .75rem;
  line-height: 1.5;
}

@media (max-width: 354px) {
  #board { border-inline-width: 0; }
}

@media (min-width: 40rem) {
  body {
    --top: max(.75rem, env(safe-area-inset-top));
    --bottom: max(.75rem, env(safe-area-inset-bottom));
    --side: 1.5rem;
    --frame-pad: .75rem;
    align-items: center;
  }
  main { display: block; }
  header { gap: .75rem; margin-bottom: 1rem; }
  .readout { font-size: 1rem; }
  footer { padding: 0; margin-top: .875rem; text-align: center; }
}

@media (max-height: 34rem) {
  body {
    --top: max(4px, env(safe-area-inset-top));
    --bottom: max(4px, env(safe-area-inset-bottom));
    --tray-height: 44px;
  }
  header { margin-bottom: .375rem; }
  h1 { font-size: 1.5rem; }
  .piece { --unit: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  .piece { transition: none; }
  #tray .piece-image { animation: none; }
  .piece:enabled:is(:active, [data-held]) { transform: none; transition: none; }
  #progress { transition: none; }
}
