:root {
  color-scheme: light;
  --case: #35283f;
  --case-deep: #1a1720;
  --case-muted: #66586f;
  --celadon: #afcbba;
  --celadon-light: #dfe9df;
  --shell: #f5f7f2;
  --copper: #8f4038;
  --copper-dark: #54272b;
  --brass: #e8b84a;
  --brass-light: #f5d982;
  --ink: #1a1720;
  --ink-soft: #514958;
  --danger: #bd3c3a;
  --focus-light: #ffd96a;
  --focus-dark: #35283f;
  --rail-height: 64px;
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --z-stage: 1;
  --z-effect: 4;
  --z-ui: 8;
  --z-dialog: 30;
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--celadon);
  color: var(--ink);
  font-family: "Segoe UI", "Noto Sans TC", system-ui, sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button,
select,
a {
  font: inherit;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  border: 0;
}

[hidden] {
  display: none !important;
}

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

.game-shell {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 320px;
  overflow: hidden;
  isolation: isolate;
  background: var(--celadon);
}

#game-canvas {
  position: absolute;
  inset: 0;
  z-index: var(--z-stage);
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  touch-action: manipulation;
}

.stage-vignette {
  position: absolute;
  inset: var(--rail-height) 0 0;
  z-index: 2;
  pointer-events: none;
  box-shadow: inset 0 -38px 74px rgba(26, 23, 32, .16);
}

.screen-flash {
  position: absolute;
  inset: 0;
  z-index: var(--z-effect);
  pointer-events: none;
  opacity: 0;
}

.screen-flash.score {
  background: rgba(245, 217, 130, .38);
  animation: score-flash 240ms var(--ease-out);
}

.screen-flash.hit {
  background: rgba(189, 60, 58, .42);
  animation: hit-flash 420ms var(--ease-out);
}

@keyframes score-flash {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

@keyframes hit-flash {
  0% { opacity: 1; }
  38% { opacity: .68; }
  100% { opacity: 0; }
}

.impact-mark {
  position: absolute;
  z-index: 6;
  width: 92px;
  height: 92px;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -50%);
}

.impact-mark span {
  position: absolute;
  inset: 50% auto auto 50%;
  width: 6px;
  height: 44px;
  border-radius: 999px;
  background: var(--danger);
  transform-origin: 50% 0;
}

.impact-mark span:nth-child(1) { transform: rotate(0deg) translateY(10px); }
.impact-mark span:nth-child(2) { transform: rotate(120deg) translateY(10px); }
.impact-mark span:nth-child(3) { transform: rotate(240deg) translateY(10px); }

.impact-mark.show {
  animation: impact-mark 520ms var(--ease-out);
}

@keyframes impact-mark {
  0% { opacity: 1; scale: .45; }
  70% { opacity: .9; scale: 1; }
  100% { opacity: 0; scale: 1.18; }
}

.game-shell.shake {
  animation: case-shake 340ms var(--ease-out);
}

@keyframes case-shake {
  0%, 100% { transform: translate3d(0, 0, 0); }
  18% { transform: translate3d(-7px, 2px, 0); }
  38% { transform: translate3d(6px, -2px, 0); }
  58% { transform: translate3d(-4px, 1px, 0); }
  78% { transform: translate3d(3px, -1px, 0); }
}

.top-rail {
  position: absolute;
  inset: 0 0 auto;
  z-index: var(--z-ui);
  min-height: calc(var(--rail-height) + env(safe-area-inset-top));
  padding:
    env(safe-area-inset-top)
    max(14px, env(safe-area-inset-right))
    0
    max(14px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: stretch;
  background: var(--case);
  color: var(--shell);
  border-bottom: 4px solid var(--brass);
}

.home-control {
  align-self: center;
  justify-self: start;
  min-width: 92px;
  min-height: 44px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--shell);
  text-decoration: none;
  font-weight: 700;
  border: 1px solid rgba(245, 247, 242, .48);
  border-radius: 8px;
  transition: color 180ms ease, background 180ms ease, border-color 180ms ease;
}

.home-control:hover {
  color: var(--case);
  background: var(--shell);
  border-color: var(--shell);
}

.work-title {
  justify-self: start;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 190px;
  margin-left: 12px;
}

.work-title img {
  border-radius: 9px;
  background: var(--shell);
}

.work-title div {
  display: grid;
  gap: 1px;
}

.work-title strong {
  font-family: Bahnschrift, "Arial Narrow", "Segoe UI", sans-serif;
  font-size: 17px;
  line-height: 1.05;
  letter-spacing: .01em;
}

.work-title span {
  color: var(--brass-light);
  font-size: 11px;
  line-height: 1.2;
}

.score-cluster {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 50%;
  translate: -50% 0;
  display: flex;
  height: 48px;
  background: var(--case-deep);
  border-radius: 8px;
  overflow: hidden;
}

.score-cell {
  min-width: 76px;
  padding: 5px 13px 4px;
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  gap: 8px;
  color: var(--celadon-light);
}

.score-cell + .score-cell {
  border-left: 1px solid rgba(245, 247, 242, .22);
}

.score-cell span {
  font-size: 10px;
  line-height: 1;
}

.score-cell strong {
  min-width: 2ch;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--shell);
}

.score-cell-current strong {
  color: var(--brass-light);
}

.score-cell-current.pulse strong {
  animation: score-tick 220ms var(--ease-out);
}

@keyframes score-tick {
  0% { transform: translateY(0) scale(1); }
  45% { transform: translateY(-3px) scale(1.24); }
  100% { transform: translateY(0) scale(1); }
}

.rail-actions {
  align-self: center;
  justify-self: end;
  display: flex;
  gap: 8px;
}

.rail-button,
.dialog-close {
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  color: var(--shell);
  background: transparent;
  border: 1px solid rgba(245, 247, 242, .48);
  border-radius: 8px;
  cursor: pointer;
  font-size: 19px;
  transition: background 180ms ease, color 180ms ease, transform 180ms var(--ease-out);
}

.rail-button:hover,
.dialog-close:hover {
  color: var(--case);
  background: var(--shell);
}

.rail-button:active,
.dialog-close:active {
  transform: translateY(1px);
}

.rail-button:disabled {
  opacity: .35;
  cursor: not-allowed;
}

.rail-button[aria-pressed="false"] #sound-icon {
  text-decoration: line-through 2px var(--brass);
}

.home-control:focus-visible,
.rail-button:focus-visible,
.wind-button:focus-visible,
.dialog-close:focus-visible {
  outline: 3px solid var(--focus-light);
  outline-offset: 3px;
}

.setting-button:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus-dark);
  outline-offset: 3px;
}

#game-canvas:focus-visible {
  outline: 3px solid var(--focus-dark);
  outline-offset: -7px;
  box-shadow: inset 0 0 0 3px var(--focus-light);
}

.flight-plate {
  position: absolute;
  left: max(24px, env(safe-area-inset-left));
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
  z-index: var(--z-ui);
  width: min(370px, calc(100% - 48px));
  padding: 22px;
  color: var(--shell);
  background: var(--case);
  border: 1px solid rgba(245, 247, 242, .38);
  border-radius: 12px;
  clip-path: polygon(0 0, calc(100% - 18px) 0, 100% 18px, 100% 100%, 18px 100%, 0 calc(100% - 18px));
}

.flight-plate::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 18px;
  height: 18px;
  background: var(--brass);
  clip-path: polygon(0 0, 100% 100%, 100% 0);
}

.start-plate {
  animation: plate-enter 520ms var(--ease-out) both;
}

.result-plate:not([hidden]) {
  animation: result-enter 420ms var(--ease-out) both;
}

@keyframes plate-enter {
  from { opacity: 0; transform: translateY(22px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes result-enter {
  from { opacity: 0; transform: translateY(26px) scale(.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.plate-number,
.movement-name {
  margin: 0;
  color: var(--brass-light);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 11px;
  letter-spacing: .06em;
}

.plate-number {
  margin-bottom: 12px;
}

.plate-heading {
  display: flex;
  align-items: center;
  gap: 14px;
}

.plate-heading img {
  flex: 0 0 auto;
  border-radius: 12px;
  background: var(--shell);
}

.movement-name {
  margin-bottom: 4px;
}

.plate-heading h1,
.result-plate h2 {
  margin: 0;
  font-family: Bahnschrift, "Arial Narrow", "Segoe UI", sans-serif;
  font-size: 30px;
  font-weight: 650;
  line-height: 1.02;
  letter-spacing: -.02em;
  text-wrap: balance;
}

.plate-copy {
  max-width: 34ch;
  margin: 16px 0;
  color: var(--celadon-light);
  font-size: 14px;
  line-height: 1.55;
  text-wrap: pretty;
}

.control-cue {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  margin-bottom: 14px;
  color: var(--shell);
  font-size: 12px;
  line-height: 1.35;
}

kbd,
.prompt-key {
  display: inline-flex;
  min-height: 26px;
  padding: 0 8px;
  align-items: center;
  justify-content: center;
  color: var(--case);
  background: var(--brass-light);
  border-radius: 5px;
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 10px;
  font-weight: 700;
  box-shadow: 0 3px 0 #9d7125;
}

.wind-button {
  width: 100%;
  min-height: 50px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--case-deep);
  background: var(--brass);
  border-radius: 8px;
  cursor: pointer;
  font-family: Bahnschrift, "Segoe UI", sans-serif;
  font-size: 16px;
  font-weight: 700;
  transition: background 180ms ease, transform 180ms var(--ease-out);
  box-shadow: 0 5px 0 #9d7125;
}

.wind-button:hover {
  background: var(--brass-light);
  transform: translateY(-1px);
}

.wind-button:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #9d7125;
}

.result-plate h2 {
  margin-bottom: 12px;
}

.medal-seal {
  position: absolute;
  top: 18px;
  right: 26px;
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  color: var(--case-deep);
  background: var(--brass);
  border-radius: 50%;
  font-size: 28px;
  rotate: 7deg;
}

.medal-seal:empty {
  display: none;
}

.result-scores {
  display: flex;
  margin: 8px 0 14px;
  border-top: 1px solid rgba(245, 247, 242, .28);
  border-bottom: 1px solid rgba(245, 247, 242, .28);
}

.result-scores > div {
  flex: 1;
  padding: 12px 4px;
  display: grid;
  gap: 4px;
}

.result-scores > div + div {
  border-left: 1px solid rgba(245, 247, 242, .28);
  padding-left: 16px;
}

.result-scores span {
  color: var(--celadon-light);
  font-size: 11px;
}

.result-scores strong {
  color: var(--shell);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 30px;
}

.record-callout {
  margin: 0 0 14px;
  padding: 9px 10px;
  color: var(--case-deep);
  background: var(--brass-light);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.gate-stamp {
  position: absolute;
  top: calc(var(--rail-height) + 42px);
  left: 50%;
  z-index: var(--z-effect);
  min-width: 150px;
  padding: 9px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  color: var(--shell);
  background: var(--copper);
  border-radius: 7px;
  opacity: 0;
  pointer-events: none;
  translate: -50% -8px;
}

.gate-stamp span {
  font-size: 12px;
  font-weight: 700;
}

.gate-stamp strong {
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 18px;
}

.gate-stamp.show {
  animation: gate-stamp 740ms var(--ease-out);
}

@keyframes gate-stamp {
  0% { opacity: 0; translate: -50% -12px; }
  18%, 72% { opacity: 1; translate: -50% 0; }
  100% { opacity: 0; translate: -50% 5px; }
}

.flight-prompt {
  position: absolute;
  left: 50%;
  bottom: max(92px, calc(env(safe-area-inset-bottom) + 84px));
  z-index: var(--z-ui);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--shell);
  background: rgba(53, 40, 63, .88);
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
  translate: -50% 0;
  animation: prompt-in 300ms var(--ease-out) both;
}

@keyframes prompt-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.music-roll {
  position: absolute;
  right: max(24px, env(safe-area-inset-right));
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
  z-index: var(--z-ui);
  min-width: 286px;
  padding: 12px 14px 14px;
  color: var(--shell);
  background: var(--case-deep);
  border-radius: 9px;
  pointer-events: none;
}

.roll-label {
  display: block;
  margin-bottom: 8px;
  color: var(--celadon-light);
  font-size: 10px;
}

.roll-track {
  display: flex;
  gap: 5px;
}

.note-key {
  flex: 1;
  height: 18px;
  background: var(--case-muted);
  border-radius: 3px;
  transition: background 180ms ease, transform 180ms var(--ease-out);
}

.note-key.active {
  background: var(--brass);
  transform: translateY(3px);
}

.version-info {
  position: absolute;
  right: max(8px, env(safe-area-inset-right));
  bottom: max(5px, env(safe-area-inset-bottom));
  z-index: 12;
  color: rgba(245, 247, 242, .62);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 9px;
  pointer-events: none;
}

.settings-dialog {
  width: min(430px, calc(100% - 28px));
  max-height: min(680px, calc(100dvh - 28px));
  padding: 0;
  color: var(--ink);
  background: var(--shell);
  border: 0;
  border-radius: 12px;
  overflow: auto;
  overscroll-behavior: contain;
  z-index: var(--z-dialog);
}

.settings-dialog::backdrop {
  background: rgba(26, 23, 32, .72);
}

.settings-dialog[open] {
  animation: dialog-in 220ms var(--ease-out);
}

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(12px) scale(.985); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.settings-dialog form {
  display: grid;
}

.dialog-head {
  min-height: 76px;
  padding: 16px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--shell);
  background: var(--case);
  border-bottom: 4px solid var(--brass);
}

.dialog-head p {
  margin: 0 0 3px;
  color: var(--brass-light);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 10px;
}

.dialog-head h2 {
  margin: 0;
  font-family: Bahnschrift, "Segoe UI", sans-serif;
  font-size: 22px;
}

.dialog-close {
  font-size: 26px;
}

.setting-row {
  min-height: 76px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  color: var(--ink);
  background: var(--shell);
  border-bottom: 1px solid #d5d9d1;
  text-align: left;
}

.setting-row > span:first-child {
  display: grid;
  gap: 3px;
}

.setting-row strong,
.instruction-block strong {
  font-size: 14px;
}

.setting-row small {
  color: var(--ink-soft);
  font-size: 11px;
}

.setting-row select {
  min-width: 150px;
  min-height: 44px;
  padding: 0 34px 0 12px;
  color: var(--ink);
  background: #fff;
  border: 1px solid #8d8692;
  border-radius: 7px;
  font-size: 16px;
}

.setting-button {
  width: 100%;
  cursor: pointer;
}

.setting-button:hover {
  background: #ebf0e9;
}

.about-mark {
  width: 30px;
  height: 30px;
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  color: var(--shell);
  background: var(--case);
  border-radius: 50%;
  font-family: Georgia, serif;
  font-size: 17px;
  font-style: italic;
  font-weight: 700;
}

/* Help remains available from Settings without covering the game board. */
.fa-help-btn {
  display: none !important;
}

.switch {
  position: relative;
  width: 52px;
  height: 30px;
  flex: 0 0 auto;
  background: #958d9a;
  border-radius: 999px;
  transition: background 180ms ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50%;
  transition: transform 180ms var(--ease-out);
}

.switch.on {
  background: var(--copper);
}

.switch.on::after {
  transform: translateX(22px);
}

.instruction-block {
  padding: 18px;
  color: var(--ink);
  background: var(--celadon-light);
}

.instruction-block p {
  max-width: 54ch;
  margin: 7px 0 0;
  color: #3e3943;
  font-size: 13px;
  line-height: 1.55;
  text-wrap: pretty;
}

.dialog-foot {
  padding: 12px 18px;
  display: flex;
  justify-content: space-between;
  color: var(--ink-soft);
  font-family: "Cascadia Mono", Consolas, monospace;
  font-size: 10px;
}

@media (max-width: 860px) {
  :root {
    --rail-height: 58px;
  }

  .top-rail {
    grid-template-columns: auto 1fr auto;
  }

  .home-control {
    min-width: 44px;
    width: 44px;
    padding: 0;
  }

  .home-word,
  .work-title {
    display: none;
  }

  .score-cluster {
    top: calc(env(safe-area-inset-top) + 6px);
    height: 46px;
  }

  .score-cell {
    min-width: 66px;
    padding-inline: 10px;
  }

  .score-cell strong {
    font-size: 20px;
  }

  .rail-actions {
    gap: 6px;
  }

  .flight-plate {
    left: max(14px, env(safe-area-inset-left));
    width: min(360px, calc(100% - 28px));
  }

  .music-roll {
    right: max(14px, env(safe-area-inset-right));
    min-width: 220px;
  }
}

@media (max-width: 520px) {
  .top-rail {
    padding-inline:
      max(8px, env(safe-area-inset-left))
      max(8px, env(safe-area-inset-right));
  }

  #sound-button {
    display: none;
  }

  .score-cell span {
    display: none;
  }

  .score-cell {
    min-width: 52px;
    grid-template-columns: 1fr;
    justify-items: center;
    padding-inline: 8px;
  }

  .flight-plate {
    bottom: max(66px, calc(env(safe-area-inset-bottom) + 54px));
    padding: 18px;
  }

  .plate-heading img {
    width: 64px;
    height: 64px;
  }

  .plate-heading h1,
  .result-plate h2 {
    font-size: 27px;
  }

  .plate-copy {
    margin: 13px 0;
  }

  .music-roll {
    left: max(14px, env(safe-area-inset-left));
    right: max(14px, env(safe-area-inset-right));
    bottom: max(12px, calc(env(safe-area-inset-bottom) + 6px));
    min-width: 0;
    padding: 8px 10px 10px;
  }

  .roll-label {
    display: none;
  }

  .note-key {
    height: 12px;
  }

  .version-info {
    display: none;
  }

  .fa-help-btn {
    display: none !important;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  :root {
    --rail-height: 50px;
  }

  .top-rail {
    min-height: calc(var(--rail-height) + env(safe-area-inset-top));
  }

  .home-control,
  .rail-button {
    width: 44px;
    height: 44px;
    min-height: 44px;
  }

  .score-cluster {
    top: calc(env(safe-area-inset-top) + 3px);
    height: 44px;
  }

  .flight-plate {
    left: max(14px, env(safe-area-inset-left));
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px));
    width: min(322px, 42vw);
    padding: 14px 16px;
  }

  .plate-number {
    margin-bottom: 6px;
  }

  .plate-heading {
    gap: 10px;
  }

  .plate-heading img {
    width: 48px;
    height: 48px;
    border-radius: 9px;
  }

  .plate-heading h1,
  .result-plate h2 {
    font-size: 23px;
  }

  .movement-name {
    font-size: 9px;
  }

  .plate-copy {
    max-width: none;
    margin: 8px 0;
    font-size: 11px;
    line-height: 1.35;
  }

  .control-cue {
    min-height: 28px;
    margin-bottom: 8px;
    font-size: 10px;
  }

  .wind-button {
    min-height: 44px;
    font-size: 14px;
  }

  .result-scores {
    margin-block: 5px 8px;
  }

  .result-scores > div {
    padding-block: 6px;
  }

  .result-scores strong {
    font-size: 22px;
  }

  .record-callout {
    margin-bottom: 8px;
    padding-block: 5px;
  }

  .medal-seal {
    top: 12px;
    right: 20px;
    width: 44px;
    height: 44px;
    font-size: 22px;
  }

  .music-roll {
    left: auto;
    right: max(14px, env(safe-area-inset-right));
    bottom: max(14px, calc(env(safe-area-inset-bottom) + 6px));
    width: min(250px, 32vw);
    min-width: 190px;
    padding: 8px 10px 10px;
  }

  .roll-label {
    display: none;
  }

  .note-key {
    height: 12px;
  }

  .flight-prompt {
    display: none;
  }

  .version-info,
  .fa-help-btn {
    display: none !important;
  }
}

@media (max-height: 380px) and (orientation: landscape) {
  .plate-copy {
    display: none;
  }

  .flight-plate {
    width: min(300px, 39vw);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }

  .game-shell.shake {
    animation: none;
  }

  .screen-flash.score {
    animation: reduced-score-feedback 180ms steps(2, end) !important;
  }

  .screen-flash.hit {
    animation: reduced-hit-feedback 240ms steps(2, end) !important;
  }

  .gate-stamp.show {
    animation: reduced-gate-feedback 620ms steps(2, end) !important;
  }

  @keyframes reduced-score-feedback {
    0%, 60% { opacity: .42; }
    100% { opacity: 0; }
  }

  @keyframes reduced-hit-feedback {
    0%, 72% { opacity: .52; }
    100% { opacity: 0; }
  }

  @keyframes reduced-gate-feedback {
    0%, 76% { opacity: 1; translate: -50% 0; }
    100% { opacity: 0; translate: -50% 0; }
  }
}
