#preview-board,
#game-board,
#hint-board {
  --case-color: #303140;
  --switch-color: #50c5ff;
  --board-width: 22.5;
  --board-height: 6.5;
  --rim-light: color-mix(in srgb, var(--case-color) 68%, #e8e5ff);
  --rim-dark: color-mix(in srgb, var(--case-color) 68%, #08070e);
  --plate: color-mix(in srgb, var(--case-color) 64%, #080910);
  position: relative;
  isolation: isolate;
  box-sizing: border-box;
  width: 100%;
  padding: clamp(7px, 1.04vw, 15px) clamp(7px, 1.12vw, 17px);
  border: 1px solid var(--rim-light);
  border-radius: clamp(8px, 1.05vw, 15px);
  background:
    linear-gradient(176deg, #fff3, transparent 16%, transparent 65%, #0003),
    linear-gradient(100deg, var(--rim-dark), var(--case-color) 48%, var(--rim-dark));
  box-shadow:
    inset 0 1px 1px #fffe,
    inset 0 -2px 4px #0008,
    0 3px 0 var(--rim-dark),
    0 6px 0 color-mix(in srgb, var(--rim-dark) 70%, #050509),
    0 10px 10px #05040970,
    0 24px 35px #08061170;
}

#game-board {
  min-width: 640px;
}

#hint-board {
  min-width: 590px;
}

.board-grid {
  --unit: calc(100cqw / var(--board-width));
  position: relative;
  container-type: inline-size;
  aspect-ratio: var(--board-width) / var(--board-height);
  width: 100%;
  min-width: 0;
  border-radius: 3px;
  background:
    repeating-linear-gradient(0deg, transparent 0 2px, #00000010 2px 3px),
    linear-gradient(160deg, var(--plate), color-mix(in srgb, var(--plate) 86%, #090818));
  box-shadow: 0 0 0 2px #08081178, inset 0 1px 5px #0009, 0 1px 0 var(--rim-light);
}

.key-slot {
  --slot-gap: max(1px, calc(var(--unit) * .043));
  position: absolute;
  isolation: isolate;
  display: block;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  appearance: none;
  border: 0;
  background: none;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

.key-slot::before {
  position: absolute;
  z-index: -1;
  inset: var(--slot-gap);
  border-radius: max(1px, calc(var(--unit) * .09));
  background: linear-gradient(145deg, #0004, #0000 38%, #fff1);
  box-shadow: inset 0 0 0 1px #08091046, 0 1px 0 #fff1;
  content: "";
}

.key-slot::after {
  position: absolute;
  z-index: 1;
  inset: var(--slot-gap);
  border-radius: max(1px, calc(var(--unit) * .095));
  pointer-events: none;
  content: "";
}

button.key-slot {
  cursor: pointer;
  touch-action: manipulation;
}

button.key-slot.has-cap {
  cursor: grab;
  touch-action: none;
}

button.key-slot:focus-visible,
button.key-slot.is-selected {
  z-index: 3;
  outline: 2px solid #ded1ff;
  outline-offset: -2px;
}

button.key-slot:hover::after,
.key-slot.is-target::after {
  box-shadow: inset 0 0 0 1.5px #c8b5ffb8, inset 0 0 10px #9371ee68;
}

.key-slot.is-target::after {
  box-shadow: inset 0 0 0 2px #e1d3ff, inset 0 0 14px #9b77ff94, 0 0 9px #a98bffb0;
}

.key-slot.is-stepped {
  clip-path: polygon(33.333% 0, 100% 0, 100% 100%, 0 100%, 0 50%, 33.333% 50%);
}

.key-slot.is-stepped::before,
.key-slot.is-stepped::after {
  border-radius: max(1px, calc(var(--unit) * .09));
}

.switch-body,
.switch-sample {
  --switch-rim: color-mix(in srgb, var(--switch-color) 31%, #dce5ff);
  --switch-shade: color-mix(in srgb, var(--switch-color) 37%, #414955);
  display: block;
  position: relative;
  box-sizing: border-box;
  border: 1px solid var(--switch-rim);
  border-radius: 14%;
  background:
    repeating-linear-gradient(90deg, #27314228 0 1px, transparent 1px 22%),
    linear-gradient(145deg, #fdffff 0%, var(--switch-rim) 35%, #8d98ac 77%, #eef2ff 100%);
  box-shadow: inset 0 0 0 1px #ffffff91, 0 1px 0 var(--switch-shade), 0 2px 2px #090a11bf;
}

.switch-body {
  position: absolute;
  top: 50%;
  left: 50%;
  width: calc(var(--unit) * .65);
  height: calc(var(--unit) * .65);
  transform: translate(-50%, -50%);
}

.key-slot.is-stepped > .switch-body {
  top: 73%;
  left: 66.667%;
}

.switch-body::before,
.switch-sample::before {
  position: absolute;
  inset: 12%;
  border: 1px solid color-mix(in srgb, var(--switch-color) 44%, #fff);
  border-radius: 17%;
  background:
    linear-gradient(90deg, #f2faff9c 0 12%, transparent 12% 88%, #f2faff73 88%),
    linear-gradient(155deg, color-mix(in srgb, var(--switch-color) 50%, #f7fdff), var(--switch-color));
  box-shadow: inset 0 1px 1px #f6ffffb5, inset 0 -1px 1px var(--switch-shade), 0 0 2px #2b365d74;
  content: "";
}

.switch-body::after {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    radial-gradient(circle at 13% 16%, #65748b 0 4%, transparent 6%),
    radial-gradient(circle at 87% 16%, #65748b 0 4%, transparent 6%),
    radial-gradient(circle at 13% 84%, #65748b 0 4%, transparent 6%),
    radial-gradient(circle at 87% 84%, #65748b 0 4%, transparent 6%);
  content: "";
}

.switch-stem,
.switch-sample::after {
  position: absolute;
  inset: 25%;
  z-index: 1;
  display: block;
  background: linear-gradient(150deg, color-mix(in srgb, var(--switch-color) 74%, #fff), var(--switch-color) 50%, color-mix(in srgb, var(--switch-color) 72%, #283357));
  clip-path: polygon(36% 0, 64% 0, 64% 35%, 100% 35%, 100% 65%, 64% 65%, 64% 100%, 36% 100%, 36% 65%, 0 65%, 0 35%, 36% 35%);
  filter: brightness(.76) saturate(1.3);
  content: "";
}

.switch-sample {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
}

.switch-sample > .switch-stem {
  display: none;
}

.keycap {
  --cap-tone: #e4e0f4;
  --cap-letter: #292437;
  --cap-wall: color-mix(in srgb, var(--cap-tone) 68%, #32284d);
  --cap-outline: color-mix(in srgb, var(--cap-tone) 54%, #31273f);
  position: relative;
  box-sizing: border-box;
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid var(--cap-outline);
  border-radius: var(--cap-radius, 5px);
  background: linear-gradient(163deg, color-mix(in srgb, var(--cap-tone) 86%, #fff) 6%, var(--cap-tone) 23%, var(--cap-wall) 78%);
  box-shadow: inset 0 1px 0 #ffffffa6, 0 2px 0 #181221a1, 0 3px 4px #0f0a1a62;
  color: var(--cap-letter);
  text-transform: none;
}

.cap-face {
  position: absolute;
  inset: 7% 8% 16%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  box-sizing: border-box;
  padding: 0 .21em;
  overflow: hidden;
  border-radius: max(1px, calc(var(--cap-radius, 5px) * .64));
  background: linear-gradient(155deg, color-mix(in srgb, var(--cap-tone) 72%, #fff), var(--cap-tone));
  box-shadow: 0 1px 1px #28193636, inset 0 1px 1px #fff9;
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, "Malgun Gothic", monospace;
  font-size: var(--cap-font, .77rem);
  font-weight: 650;
  line-height: 1.06;
  white-space: pre-line;
  text-align: left;
}

.cap-label,
.cap-secondary {
  display: block;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.cap-secondary {
  color: color-mix(in srgb, var(--cap-letter) 79%, #9487ac);
  font-size: .78em;
}

.cap-label.is-long {
  font-size: .87em;
}

.key-slot > .keycap {
  --cap-font: clamp(.3rem, calc(var(--unit) * .31), .77rem);
  --cap-radius: max(2px, calc(var(--unit) * .115));
  position: absolute;
  inset: var(--slot-gap);
  width: auto;
  height: auto;
  pointer-events: none;
}

.keycap[data-group="function"],
.keycap[data-group="navigation"] {
  --cap-tone: #c9bde9;
}

.keycap[data-group="numpad"] {
  --cap-tone: #d9d0f0;
}

.keycap.is-stepped {
  clip-path: polygon(33.333% 0, 100% 0, 100% 100%, 0 100%, 0 50%, 33.333% 50%);
}

.keycap.is-stepped > .cap-face {
  inset: 6% 6% 10%;
  padding: 0 .46em;
  justify-content: flex-end;
  padding-bottom: 11%;
  clip-path: polygon(34% 0, 100% 0, 100% 100%, 0 100%, 0 56%, 34% 56%);
}

.key-slot.is-snapping {
  z-index: 2;
}

.key-slot.is-snapping > .keycap {
  animation: seat-keycap 290ms cubic-bezier(.18, .83, .2, 1);
}

.key-slot.is-snapping::after {
  animation: seat-flash 300ms ease-out;
}

@keyframes seat-keycap {
  0% { transform: translateY(-14%) scale(1.14); filter: brightness(1.24); }
  36% { transform: translateY(7%) scale(.95); filter: brightness(.8); }
  63% { transform: translateY(-2%) scale(1.025); filter: brightness(1.1); }
  100% { transform: none; filter: none; }
}

@keyframes seat-flash {
  from { box-shadow: inset 0 0 0 2px #f2eaff, 0 0 10px 3px #bf9cffda; opacity: 1; }
  to { box-shadow: inset 0 0 0 1px #f2eaff50, 0 0 10px 6px #bf9cff00; opacity: 0; }
}

.key-slot.is-wrong > .keycap {
  --cap-tone: #ec899f;
}

.key-slot.is-wrong::after {
  box-shadow: inset 0 0 0 2px #ff6179, 0 0 10px 2px #ff355f98;
  animation: wrong-keycap 850ms ease-in-out infinite alternate;
}

@keyframes wrong-keycap {
  from { opacity: .42; }
  to { opacity: 1; }
}

#game-board.is-complete .key-slot {
  --led-color: #b48bff;
  --led-delay: 0ms;
}

#game-board.is-complete .key-slot:nth-child(4n+2) {
  --led-color: #78e5ff;
  --led-delay: 130ms;
}

#game-board.is-complete .key-slot:nth-child(4n+3) {
  --led-color: #e784ff;
  --led-delay: 260ms;
}

#game-board.is-complete .key-slot:nth-child(4n) {
  --led-color: #a2f3bf;
  --led-delay: 390ms;
}

#game-board.is-complete .key-slot::after {
  background: color-mix(in srgb, var(--led-color) 35%, transparent);
  box-shadow: inset 0 0 2px var(--led-color), 0 0 9px 2px var(--led-color);
  animation: board-led 1s var(--led-delay) 3 both;
}

@keyframes board-led {
  0%, 100% { opacity: 0; }
  21%, 60% { opacity: .88; }
  42% { opacity: .21; }
}

.tray-field {
  max-height: clamp(150px, 29vh, 230px);
  overflow-y: auto;
  scrollbar-color: #785d92 #211a2a;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
}

#tray {
  --tray-unit: clamp(36px, 4.35vw, 49px);
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  align-items: center;
  gap: clamp(6px, 1vw, 13px);
  box-sizing: border-box;
  min-height: calc(var(--tray-unit) + 1.2rem);
  padding: 9px 5px 12px;
}

.tray-key {
  position: relative;
  display: block;
  flex: none;
  width: calc(var(--tray-unit) * var(--cap-width, 1));
  height: calc(var(--tray-unit) * var(--cap-height, 1));
  margin: 0;
  padding: 0;
  appearance: none;
  border: 0;
  border-radius: 6px;
  background: none;
  transform: rotate(var(--tray-rotation, 0deg));
  transition: transform 135ms ease, filter 135ms ease;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.tray-key > .keycap {
  pointer-events: none;
}

.tray-key[hidden] {
  display: none;
}

.tray-key:is(:hover, :focus-visible, .is-selected) {
  z-index: 2;
  transform: translateY(-3px) rotate(0);
}

.tray-key:focus-visible {
  outline: 2px solid #e0d3ff;
  outline-offset: 3px;
}

.tray-key.is-selected {
  filter: drop-shadow(0 0 5px #c4a8ff);
}

.tray-key.is-selected > .keycap {
  --cap-tone: #dfcbff;
}

.is-dragging > .keycap {
  opacity: .43;
}

body {
  -webkit-user-select: none;
  user-select: none;
}

#record-name {
  -webkit-user-select: text;
  user-select: text;
}

.is-dragging-key,
.is-dragging-key button {
  cursor: grabbing;
}

.drag-ghost {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  cursor: grabbing;
  filter: drop-shadow(0 9px 9px #0d092495) drop-shadow(0 0 5px #b18cffa9);
}

.drag-ghost > .keycap {
  transform: translate(-50%, -50%) rotate(-4deg) scale(1.04);
}

@media (pointer: coarse) {
  .tray-field {
    padding-inline-end: max(25px, 6vw);
    touch-action: pan-y;
  }
}

@media (max-width: 520px) {
  .switch-body {
    border-width: .5px;
  }

  .switch-body::before {
    border-width: .5px;
  }

  #preview-board .board-grid {
    box-shadow: 0 0 0 1px #08081178, inset 0 1px 4px #0009, 0 1px 0 var(--rim-light);
  }
}

@media (prefers-reduced-motion: reduce) {
  .tray-key {
    transition: none;
  }

  .key-slot.is-snapping > .keycap,
  .key-slot.is-snapping::after,
  .key-slot.is-wrong::after {
    animation: none;
  }

  #game-board.is-complete .key-slot::after {
    animation: board-led 400ms ease-out both;
  }
}
