:root {
  color-scheme: dark;
  --quarry: #17332d;
  --workbench: #285047;
  --limestone: #e6e1d2;
  --terracotta: #c35f3d;
  --brass: #d6a83f;
  --ink: #10211d;
  --mist: #f2f3ea;
  --muted: #b8c6bd;
  --line: #557069;
  --danger: #e18464;
  --focus: #ffd86b;
  --header-height: 68px;
  --z-header: 20;
  --z-dropdown: 30;
  --z-feedback: 10;
}

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

html {
  background: var(--ink);
  font-family:
    "Noto Sans TC",
    "Noto Sans",
    "PingFang TC",
    "Hiragino Sans",
    Arial,
    sans-serif;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  background: var(--quarry);
  color: var(--mist);
  -webkit-tap-highlight-color: transparent;
}

button,
select,
summary,
a {
  font: inherit;
}

button,
summary,
.workshop-stage {
  touch-action: manipulation;
}

button {
  color: inherit;
}

a {
  color: inherit;
}

button:focus-visible,
select:focus-visible,
summary:focus-visible,
a:focus-visible,
canvas:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

#fa-help-btn {
  width: 44px !important;
  min-width: 44px;
  height: 44px !important;
  min-height: 44px;
}

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

.skip-link {
  position: fixed;
  top: 10px;
  left: 50%;
  z-index: calc(var(--z-dropdown) + 1);
  min-height: 44px;
  padding: 11px 18px;
  background: var(--focus);
  color: var(--ink);
  font-weight: 800;
  text-decoration: none;
  transform: translate(-50%, -160%);
  transition: transform 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

.skip-link:focus {
  transform: translate(-50%, 0);
}

#game-root {
  display: grid;
  min-height: 100vh;
  min-height: 100dvh;
  grid-template-rows: var(--header-height) minmax(0, 1fr);
}

.app-header {
  position: relative;
  z-index: var(--z-header);
  display: grid;
  align-items: center;
  grid-template-columns: 1fr auto 1fr;
  min-width: 0;
  min-height: var(--header-height);
  padding: 10px clamp(12px, 2.5vw, 32px);
  border-bottom: 2px solid var(--line);
  background: var(--ink);
}

.back-link,
.settings > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  gap: 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--mist);
  cursor: pointer;
  font-size: 0.94rem;
  font-weight: 750;
  text-decoration: none;
  transition:
    color 180ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

.back-link {
  justify-self: start;
  padding: 0 10px;
}

.back-link span:first-child {
  color: var(--brass);
  font-size: 1.2rem;
}

.back-link:hover,
.settings > summary:hover {
  background: var(--workbench);
  color: #fff;
}

.header-mark {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  height: 32px;
  gap: 4px;
}

.header-mark span {
  display: block;
  width: 25px;
  border: 2px solid var(--brass);
  background: var(--workbench);
}

.header-mark span:nth-child(1) {
  height: 10px;
  background: var(--terracotta);
}

.header-mark span:nth-child(2) {
  height: 18px;
  background: var(--limestone);
}

.header-mark span:nth-child(3) {
  height: 26px;
  background: var(--workbench);
}

.settings {
  position: relative;
  justify-self: end;
}

.settings > summary {
  padding: 0 11px;
  list-style: none;
}

.settings > summary::-webkit-details-marker {
  display: none;
}

.settings[open] > summary {
  background: var(--workbench);
}

.settings-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: var(--z-dropdown);
  display: grid;
  width: min(280px, calc(100vw - 24px));
  gap: 9px;
  padding: 16px;
  border: 2px solid var(--brass);
  border-radius: 10px;
  background: var(--ink);
  box-shadow: 0 6px 0 rgba(7, 18, 15, 0.62);
}

.settings-panel label {
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.settings-panel select {
  width: 100%;
  min-height: 44px;
  padding: 0 38px 0 12px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--quarry);
  color: var(--mist);
  font-size: 16px;
}

.settings-version {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 4px;
  color: var(--muted);
  font-size: 0.82rem;
}

.settings-version strong {
  color: var(--brass);
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.game-layout {
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-columns: minmax(290px, 350px) minmax(0, 1fr);
}

.control-rail {
  display: flex;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  padding: clamp(22px, 4vh, 42px) clamp(22px, 3vw, 38px) 20px;
  border-right: 2px solid var(--line);
  background: var(--quarry);
}

.title-block {
  max-width: 30rem;
}

.workshop-label {
  margin: 0 0 10px;
  color: var(--brass);
  font-family:
    "Roboto Slab",
    "Noto Serif TC",
    Georgia,
    serif;
  font-size: 0.94rem;
  font-weight: 700;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  max-width: 12ch;
  margin-bottom: 10px;
  color: var(--limestone);
  font-family:
    "Roboto Slab",
    "Noto Serif TC",
    Georgia,
    serif;
  font-size: clamp(2.5rem, 5vw, 4.8rem);
  font-weight: 780;
  letter-spacing: -0.035em;
  line-height: 0.96;
  text-wrap: balance;
}

.app-subtitle {
  max-width: 30ch;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.55;
  text-wrap: pretty;
}

.score-strip {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  margin: clamp(20px, 4vh, 36px) 0 0;
  padding: 14px 0;
  border-top: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
}

.score-strip > div {
  display: grid;
  min-width: 0;
  flex: 1;
  gap: 4px;
}

.score-strip > div + div {
  padding-left: 13px;
  border-left: 1px solid var(--line);
}

.score-strip dt {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.score-strip dd {
  margin: 0;
  color: var(--limestone);
  font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  font-size: clamp(1.2rem, 2.8vw, 1.75rem);
  font-variant-numeric: tabular-nums;
  font-weight: 760;
  line-height: 1;
}

.score-strip > div:last-child dd {
  color: var(--brass);
}

.score-strip dd.bump {
  animation: score-bump 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes score-bump {
  50% {
    transform: translateY(-3px);
  }
}

.phase-row {
  display: flex;
  align-items: center;
  min-height: 44px;
  gap: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.phase-row strong {
  margin-left: auto;
  color: var(--mist);
}

.phase-indicator {
  width: 10px;
  height: 10px;
  border: 2px solid var(--brass);
  border-radius: 50%;
  background: transparent;
}

#game-root[data-phase="running"] .phase-indicator {
  background: var(--brass);
}

#game-root[data-phase="gameover"] .phase-indicator {
  border-color: var(--danger);
  background: var(--danger);
}

.start-panel,
.result-panel {
  display: grid;
  align-content: end;
  gap: 15px;
  margin-top: auto;
  padding-top: 18px;
}

.start-panel[hidden],
.result-panel[hidden] {
  display: none;
}

.start-panel p {
  max-width: 34ch;
  margin-bottom: 0;
  color: var(--limestone);
  font-size: 0.96rem;
  line-height: 1.55;
  text-wrap: pretty;
}

.primary-action {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 52px;
  gap: 16px;
  padding: 12px 16px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--brass);
  box-shadow: 0 4px 0 var(--ink);
  color: var(--ink);
  cursor: pointer;
  font-weight: 850;
  transition:
    transform 160ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.primary-action:hover {
  background: #edc661;
  transform: translateY(-2px);
}

.primary-action:active {
  box-shadow: 0 1px 0 var(--ink);
  transform: translateY(3px);
}

.result-label {
  margin-bottom: -10px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.result-score {
  color: var(--limestone);
  font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  font-size: 3.1rem;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.result-stats {
  display: flex;
  gap: 12px;
}

.result-stats > div {
  display: grid;
  min-width: 0;
  flex: 1;
  gap: 3px;
}

.result-stats span {
  color: var(--muted);
  font-size: 0.72rem;
}

.result-stats strong {
  color: var(--brass);
  font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  font-size: 1.02rem;
  font-variant-numeric: tabular-nums;
}

.new-record {
  width: max-content;
  margin: -4px 0 0;
  padding: 5px 8px;
  border: 2px solid var(--terracotta);
  color: var(--limestone);
  font-size: 0.82rem;
  font-weight: 850;
}

.input-hint {
  display: flex;
  align-items: center;
  min-height: 44px;
  gap: 9px;
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.space-key {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-bottom-width: 3px;
  border-radius: 5px;
  color: var(--limestone);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.7rem;
}

.workshop {
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  padding: clamp(12px, 2vw, 24px);
  background: var(--workbench);
}

.workshop-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  min-height: 50px;
  gap: 16px;
}

.stage-heading {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 10px;
}

.stage-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: 2px solid var(--brass);
  border-radius: 50%;
  color: var(--brass);
  font-weight: 900;
}

.stage-heading h2 {
  margin-bottom: 2px;
  color: var(--mist);
  font-size: 0.96rem;
  line-height: 1.1;
}

.stage-heading p {
  max-width: 45ch;
  margin-bottom: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sound-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  gap: 7px;
  padding: 0 11px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--mist);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 750;
  transition:
    border-color 160ms cubic-bezier(0.22, 1, 0.36, 1),
    background-color 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.sound-toggle:hover {
  border-color: var(--brass);
  background: var(--quarry);
}

.sound-toggle[aria-pressed="false"] {
  color: var(--muted);
}

.sound-icon {
  color: var(--brass);
  font-size: 1.05rem;
}

.workshop-stage {
  position: relative;
  min-width: 0;
  min-height: 220px;
  overflow: hidden;
  border: 2px solid var(--ink);
  border-radius: 14px;
  background: var(--quarry);
  box-shadow: 0 5px 0 rgba(7, 18, 15, 0.72);
  cursor: crosshair;
}

.workshop-stage[data-phase="idle"],
.workshop-stage[data-phase="gameover"] {
  cursor: default;
}

.workshop-stage[data-feedback="perfect"] {
  animation: settle-perfect 230ms cubic-bezier(0.22, 1, 0.36, 1);
}

.workshop-stage[data-feedback="good"] {
  animation: settle-good 190ms cubic-bezier(0.22, 1, 0.36, 1);
}

.workshop-stage[data-feedback="rough"],
.workshop-stage[data-feedback="missed"] {
  animation: stone-knock 220ms cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes settle-perfect {
  50% {
    border-color: var(--brass);
    transform: translateY(2px);
  }
}

@keyframes settle-good {
  50% {
    border-color: var(--limestone);
  }
}

@keyframes stone-knock {
  30% {
    transform: translateX(-3px);
  }
  70% {
    transform: translateX(2px);
  }
}

#game-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
}

#game-canvas:focus-visible {
  outline-offset: -7px;
}

.feedback-banner {
  position: absolute;
  top: 18px;
  left: 50%;
  z-index: var(--z-feedback);
  min-width: 110px;
  padding: 8px 12px;
  border: 2px solid var(--brass);
  border-radius: 7px;
  background: var(--ink);
  color: var(--limestone);
  font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  font-size: 0.82rem;
  font-weight: 800;
  opacity: 0;
  text-align: center;
  transform: translate(-50%, -8px);
  transition:
    opacity 150ms cubic-bezier(0.22, 1, 0.36, 1),
    transform 150ms cubic-bezier(0.22, 1, 0.36, 1);
}

.feedback-banner.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.alignment-readout {
  position: absolute;
  top: 15px;
  right: 15px;
  display: flex;
  align-items: baseline;
  gap: 7px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(16, 33, 29, 0.88);
  color: var(--muted);
  font-size: 0.7rem;
}

.alignment-readout strong {
  color: var(--brass);
  font-family: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

.version-corner {
  position: fixed;
  right: max(8px, env(safe-area-inset-right));
  bottom: max(6px, env(safe-area-inset-bottom));
  z-index: 4;
  color: rgba(242, 243, 234, 0.54);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 10px;
  pointer-events: none;
}

@media (max-width: 760px) and (orientation: portrait) {
  :root {
    --header-height: 58px;
  }

  .app-header {
    padding: 7px 10px;
  }

  .header-mark {
    transform: scale(0.82);
  }

  .back-link,
  .settings > summary {
    padding: 0 8px;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
  }

  .control-rail {
    padding: 14px 16px 10px;
    border-right: 0;
    border-bottom: 2px solid var(--line);
  }

  .title-block {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: baseline;
    gap: 0 12px;
  }

  .workshop-label {
    grid-column: 1 / -1;
    margin-bottom: 4px;
    font-size: 0.76rem;
  }

  h1 {
    max-width: none;
    margin-bottom: 0;
    font-size: 2.15rem;
    line-height: 1;
  }

  .app-subtitle {
    align-self: end;
    margin-bottom: 3px;
    font-size: 0.84rem;
    line-height: 1.3;
  }

  .score-strip {
    margin-top: 11px;
    padding: 9px 0;
  }

  .score-strip dt {
    font-size: 0.68rem;
  }

  .score-strip dd {
    font-size: 1.14rem;
  }

  .phase-row {
    min-height: 38px;
    font-size: 0.78rem;
  }

  .start-panel {
    grid-template-columns: minmax(0, 1fr) minmax(145px, 0.75fr);
    align-items: center;
    gap: 12px;
    padding-top: 4px;
  }

  .start-panel p {
    font-size: 0.78rem;
    line-height: 1.35;
  }

  .primary-action {
    min-height: 48px;
  }

  .input-hint {
    min-height: 32px;
    margin-top: 5px;
    font-size: 0.7rem;
  }

  .result-panel {
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 7px 12px;
    padding-top: 3px;
  }

  .result-label {
    margin: 0;
  }

  .result-score {
    font-size: 2rem;
  }

  .result-stats {
    grid-column: 1 / -1;
  }

  .result-panel .primary-action {
    grid-column: 2;
    grid-row: 1 / span 2;
  }

  .new-record {
    grid-column: 1 / -1;
    margin: 0;
  }

  .workshop {
    gap: 7px;
    padding: 8px 10px 12px;
  }

  .workshop-toolbar {
    min-height: 44px;
  }

  .stage-heading p {
    max-width: 26ch;
  }

  .sound-toggle {
    padding: 0 9px;
  }

  .sound-toggle #sound-label {
    display: none;
  }
}

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

  .app-header {
    min-height: 50px;
    padding: 3px 12px;
  }

  .back-link,
  .settings > summary {
    min-height: 44px;
    padding: 0 8px;
  }

  .header-mark {
    height: 28px;
    transform: scale(0.72);
  }

  .game-layout {
    grid-template-columns: clamp(220px, 31vw, 285px) minmax(0, 1fr);
  }

  .control-rail {
    padding: 10px 15px 7px;
    border-right: 2px solid var(--line);
    border-bottom: 0;
  }

  .workshop-label,
  .app-subtitle {
    display: none;
  }

  h1 {
    max-width: none;
    margin-bottom: 0;
    font-size: clamp(1.55rem, 4.8vh, 2rem);
    line-height: 1;
  }

  .score-strip {
    margin-top: 8px;
    padding: 7px 0;
  }

  .score-strip dt {
    font-size: 0.62rem;
  }

  .score-strip dd {
    font-size: 1rem;
  }

  .score-strip > div + div {
    padding-left: 8px;
  }

  .phase-row {
    min-height: 34px;
    font-size: 0.72rem;
  }

  .start-panel,
  .result-panel {
    gap: 7px;
    padding-top: 2px;
  }

  .start-panel p {
    max-width: none;
    font-size: 0.7rem;
    line-height: 1.25;
  }

  .primary-action {
    min-height: 44px;
    padding: 8px 12px;
    font-size: 0.8rem;
  }

  .input-hint {
    min-height: 24px;
    margin-top: 3px;
    font-size: 0.65rem;
  }

  .space-key {
    display: none;
  }

  .result-label {
    margin: 0;
    font-size: 0.7rem;
  }

  .result-score {
    font-size: 1.8rem;
  }

  .result-stats {
    gap: 7px;
  }

  .result-stats span {
    font-size: 0.6rem;
  }

  .result-stats strong {
    font-size: 0.82rem;
  }

  .new-record {
    margin: 0;
    padding: 3px 6px;
    font-size: 0.68rem;
  }

  .workshop {
    gap: 4px;
    padding: 5px 9px 8px;
  }

  .workshop-toolbar {
    min-height: 44px;
  }

  .stage-heading {
    gap: 7px;
  }

  .stage-number {
    width: 28px;
    height: 28px;
    border-width: 1px;
    font-size: 0.76rem;
  }

  .stage-heading h2 {
    font-size: 0.78rem;
  }

  .stage-heading p {
    display: none;
  }

  .sound-toggle {
    min-height: 44px;
    padding: 0 9px;
  }

  .sound-toggle #sound-label {
    display: none;
  }

  .workshop-stage {
    min-height: 170px;
  }

  .feedback-banner {
    top: 8px;
  }

  .alignment-readout {
    top: 8px;
    right: 8px;
  }

  #game-root[data-phase="gameover"] .title-block {
    display: none;
  }

  #game-root[data-phase="gameover"] .score-strip {
    margin-top: 0;
  }
}

@media (max-width: 620px) and (max-height: 380px) and (orientation: landscape) {
  .game-layout {
    grid-template-columns: 205px minmax(0, 1fr);
  }

  .control-rail {
    padding-inline: 10px;
  }

  .start-panel p,
  .input-hint {
    display: none;
  }

  .score-strip {
    margin-top: 6px;
  }

  .workshop-toolbar {
    min-height: 44px;
  }

  .stage-heading h2 {
    font-size: 0.72rem;
  }

  .sound-toggle {
    min-height: 44px;
  }
}

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