@font-face {
  font-family: "Atkinson";
  src: url("../assets/fonts/AtkinsonHyperlegible-Regular.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "Atkinson";
  src: url("../assets/fonts/AtkinsonHyperlegible-Bold.ttf") format("truetype");
  font-weight: 700;
  font-display: swap;
}

:root {
  --ink: #1c1814;
  --paper: #f3e6c8;
  --paper-deep: #e7d4a8;
  --brass: #c4a056;
  --brass-hot: #ffe08a;
  --wood-dark: #241c18;
  --ok: #c4a056;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--wood-dark);
  color: var(--ink);
  font-family: "Atkinson", "Segoe UI", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

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

#app {
  height: 100%;
  display: grid;
  place-items: center;
}

#scene {
  position: relative;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  overflow: hidden;
  background: #6e4a2e;
}

#desk {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  pointer-events: none;
}

.sheet {
  background: var(--paper);
  border: 4px solid var(--ink);
  box-shadow: 6px 7px 0 rgba(0, 0, 0, 0.28);
  border-radius: 0;
}

#chart {
  position: absolute;
  left: 2.4%;
  top: 6%;
  width: 19.5%;
  height: 54%;
  z-index: 3;
  padding: 0.55rem 0.45rem 0.65rem;
  transition: opacity 0.2s ease, transform 0.2s ease;
  background-image: linear-gradient(180deg, #f7edd4 0%, var(--paper) 100%);
}

#chart.is-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-12px);
}

#chart-body {
  height: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 0.92fr;
  gap: 0.2rem 0.28rem;
}

.chart-col {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
}

.chart-row {
  display: flex;
  align-items: center;
  gap: 0.28rem;
  min-height: 0;
}

.chart-ch {
  font-weight: 700;
  font-size: clamp(13px, 1.55vw, 22px);
  width: 0.95em;
  flex: 0 0 auto;
  line-height: 1;
}

.chart-m {
  display: flex;
  align-items: center;
  gap: 0.08em;
  transform: scale(0.82);
  transform-origin: left center;
}

.chart-nums {
  padding-left: 0.22rem;
  border-left: 2px solid rgba(28, 24, 20, 0.35);
  justify-content: space-between;
}

.chart-nums .chart-ch {
  width: 0.72em;
  font-size: clamp(12px, 1.4vw, 18px);
}

.chart-nums .chart-m {
  transform: scale(0.58);
  gap: 0.04em;
}

.chart-nums .dah {
  width: 0.92em;
}

.chart-nums .dit {
  width: 0.34em;
  height: 0.34em;
}

#chart-toggle {
  position: absolute;
  left: 0.6%;
  top: 8%;
  z-index: 6;
  width: 28px;
  height: 46px;
  padding: 0;
  border: 3px solid var(--ink);
  background: var(--paper);
  cursor: pointer;
  border-radius: 0 6px 6px 0;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.18);
}

#chart-toggle::before {
  content: "";
  display: block;
  width: 8px;
  height: 8px;
  margin: 0 auto;
  border-left: 3px solid var(--ink);
  border-bottom: 3px solid var(--ink);
  transform: rotate(45deg);
}

#scene.chart-off #chart-toggle::before {
  transform: rotate(-135deg);
  margin-top: 2px;
}

#paper-wrap,
#card-wrap {
  position: absolute;
  z-index: 3;
}

#paper-wrap {
  left: 24%;
  top: 8%;
  width: 38%;
  height: 24%;
}

#card-wrap {
  left: 26%;
  top: 33%;
  width: 52%;
  height: 30%;
}

#paper {
  transform: perspective(1000px) rotateX(12deg);
}

#card {
  transform: perspective(1000px) rotateX(7deg);
  box-shadow: 6px 8px 0 rgba(0, 0, 0, 0.3);
  border-width: 4px;
}

#paper,
#card {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background-image:
    linear-gradient(180deg, rgba(255, 255, 255, 0.28), transparent 42%),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 27px,
      rgba(28, 24, 20, 0.04) 28px
    );
}

#cue {
  flex: 0 0 auto;
  margin: 0;
  padding: 0.42rem 0.75rem 0.1rem;
  text-align: center;
  font-size: clamp(14px, 1.55vw, 22px);
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: 0.01em;
  color: var(--ink);
}

#cue:empty {
  display: none;
}

.content {
  width: 100%;
  height: 100%;
  padding: 0.7rem 0.9rem;
}

#card-content {
  flex: 1;
  min-height: 0;
  height: auto;
}

.paper-morse,
.card-morse {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 0.7em 1em;
  font-size: clamp(18px, 2.35vw, 34px);
}

.paper-text,
.paper-timing,
.card-letters {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.2;
  text-transform: uppercase;
}

.paper-text {
  font-size: clamp(32px, 4.6vw, 62px);
}

.paper-timing {
  font-size: clamp(64px, 9vw, 120px);
}

.card-letters {
  font-size: clamp(26px, 3.6vw, 48px);
  justify-content: flex-start;
  align-content: center;
  flex-wrap: wrap;
  letter-spacing: 0.12em;
}

#scene.waiting .caret,
#scene.mode-wait .caret {
  display: none;
}

.typed {
  white-space: pre-wrap;
}

.timing-letter {
  font-weight: 700;
}

.paper-blank {
  background: transparent;
}

.morse-word,
.morse-letter {
  display: inline-flex;
  align-items: center;
  gap: 0.22em;
}

.morse-word {
  gap: 0.55em;
}

.dit,
.dah {
  display: inline-block;
  background: var(--ink);
  border: 2px solid var(--ink);
  flex: 0 0 auto;
}

.dit {
  width: 0.42em;
  height: 0.42em;
  border-radius: 50%;
}

.dah {
  width: 1.18em;
  height: 0.36em;
  border-radius: 0.18em;
}

.morse-letter.current .dit,
.morse-letter.current .dah {
  background: var(--brass);
  border-color: var(--ink);
}

.caret {
  display: inline-block;
  width: 4px;
  height: 0.82em;
  margin-left: 0.1em;
  background: var(--ink);
  animation: blink 1s steps(1) infinite;
  transform: translateY(0.08em);
}

.card-morse .caret {
  align-self: center;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.card-pills {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.7rem;
}

.pill {
  position: relative;
  height: 2.4rem;
  border: 3px solid var(--ink);
  background: #efe3c4;
  overflow: hidden;
  border-radius: 999px;
}

.dit-pill {
  width: 2.4rem;
}

.dah-pill {
  width: 6.2rem;
}

.pill .fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0;
  background: var(--brass);
}

.pill.now {
  box-shadow: 0 0 0 3px var(--brass-hot);
}

.pill.good {
  background: #d9c07a;
}

.pill.good .fill {
  width: 100%;
  background: #b48b3a;
}

.pill.bad {
  background: #e7c4b0;
}

#machines {
  position: absolute;
  left: 26%;
  top: 64%;
  width: 52%;
  height: 28%;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  pointer-events: none;
}

#key,
#sounder {
  position: relative;
  z-index: 5;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
  pointer-events: auto;
}

#key {
  width: 32%;
  aspect-ratio: 1;
}

#sounder {
  width: 40%;
  aspect-ratio: 3 / 2;
}

.machine-label {
  position: absolute;
  left: 50%;
  bottom: 13%;
  z-index: 2;
  transform: translateX(-50%);
  padding: 0.14rem 0.5rem 0.18rem;
  background: var(--paper);
  border: 2px solid var(--ink);
  box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.18);
  font-size: clamp(13px, 1.35vw, 18px);
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
  pointer-events: none;
}

#key .machine-label {
  bottom: 8%;
  font-size: clamp(12px, 1.2vw, 16px);
}

#scene.mode-send #key,
#scene.mode-timing #key,
#scene.waiting #key {
  transform: none;
}

#key.disabled {
  cursor: default;
}

#key img,
#sounder img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center bottom;
  pointer-events: none;
  opacity: 0;
  filter: none;
}

#key img.up,
#sounder img.rest {
  opacity: 1;
}

#key.down img.up {
  opacity: 0;
}

#key.down img.down {
  opacity: 1;
}

#key.down.lit img.down {
  opacity: 0;
}

#key.down.lit img.lit {
  opacity: 1;
}

#sounder.live img.rest {
  opacity: 0;
}

#sounder.live img.live {
  opacity: 1;
}

#key.lit {
  filter: none;
}

#sounder.live {
  filter: none;
}

#scene.waiting #key {
  animation: wait-pulse 1.4s ease-in-out infinite;
}

@keyframes wait-pulse {
  50% {
    transform: translateY(2px);
  }
}

#practice {
  position: absolute;
  left: 2.4%;
  top: 62%;
  width: 19.5%;
  z-index: 4;
  padding: 0.4rem 0.5rem 0.55rem;
}

#practice-list {
  display: flex;
  flex-direction: column;
  gap: 0.16rem;
}

.practice-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: clamp(12px, 1.35vw, 16px);
  font-weight: 700;
  cursor: pointer;
  line-height: 1.2;
}

.practice-row input {
  width: 1.05em;
  height: 1.05em;
  margin: 0;
  accent-color: var(--brass);
  flex: 0 0 auto;
}

.practice-row.is-locked {
  opacity: 0.42;
  cursor: default;
}

.practice-row.is-locked input {
  pointer-events: none;
}

#rank {
  margin-top: 0.55rem;
  padding-top: 0.45rem;
  border-top: 3px solid var(--ink);
}

#rank-name {
  font-weight: 700;
  font-size: clamp(13px, 1.4vw, 17px);
  line-height: 1.2;
}

#rank-stats {
  margin-top: 0.12rem;
  font-size: clamp(11px, 1.15vw, 14px);
  font-weight: 700;
  line-height: 1.25;
  color: #4a3d30;
}

#rank-bar {
  margin-top: 0.35rem;
  height: 8px;
  border: 2px solid var(--ink);
  background: #efe3c4;
  overflow: hidden;
}

#rank-bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--brass);
}

#rank.is-master #rank-name {
  color: #6b4a12;
}

#rank.is-up {
  animation: rank-flash 0.8s ease;
}

@keyframes rank-flash {
  30% {
    background: #ffe08a;
  }
}

#help-btn {
  position: absolute;
  left: 1.1%;
  bottom: 1.8%;
  z-index: 16;
  width: 48px;
  height: 48px;
  border: 3px solid var(--ink);
  background: var(--brass);
  color: var(--ink);
  font-family: inherit;
  font-size: 1.6rem;
  font-weight: 700;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.2);
  line-height: 1;
}

#help-btn:hover {
  background: var(--brass-hot);
}

#trailpost {
  position: absolute;
  right: 1.2%;
  bottom: 2.4%;
  z-index: 16;
  font-size: 11px;
  color: rgba(247, 241, 225, 0.62);
  text-decoration: none;
  letter-spacing: 0.02em;
}

#trailpost:hover {
  color: #f7f1e1;
  text-decoration: underline;
}

#gate[hidden] {
  display: none !important;
}

#welcome-overlay {
  position: absolute;
  inset: 0;
  z-index: 19;
  background: rgba(28, 24, 20, 0.46);
  display: grid;
  place-items: center;
  padding: 4%;
}

#welcome-overlay[hidden] {
  display: none !important;
}

#welcome {
  width: min(640px, 100%);
  max-height: 90%;
  overflow: auto;
  background: var(--paper);
  border: 4px solid var(--ink);
  box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.25);
  padding: 1.35rem 1.5rem 1.5rem;
  -webkit-user-select: text;
  user-select: text;
}

#welcome h2 {
  font-size: 1.7rem;
  margin: 0 0 0.45rem;
}

#welcome h3 {
  font-size: 1.15rem;
  margin: 0.9rem 0 0.35rem;
}

#welcome p,
#welcome li {
  font-size: 1.05rem;
  line-height: 1.45;
}

#welcome p {
  margin: 0 0 0.55rem;
}

#welcome ul {
  margin: 0 0 0.85rem;
  padding-left: 1.2rem;
}

#welcome li {
  margin: 0.28rem 0;
}

#welcome-start {
  font-family: inherit;
  font-size: 1.12rem;
  font-weight: 700;
  padding: 0.65rem 1.05rem;
  border: 3px solid var(--ink);
  background: var(--brass);
  cursor: pointer;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.18);
}

#welcome-start:hover,
#welcome-start:focus {
  background: var(--brass-hot);
}

#help-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(28, 24, 20, 0.38);
  display: grid;
  place-items: center;
  padding: 4%;
}

#help-overlay[hidden] {
  display: none !important;
}

#help {
  width: min(720px, 100%);
  max-height: 86%;
  overflow: auto;
  background: var(--paper);
  border: 4px solid var(--ink);
  box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.25);
  padding: 1.4rem 1.6rem 1.6rem;
  position: relative;
  -webkit-user-select: text;
  user-select: text;
}

#help h2 {
  font-size: 1.35rem;
  margin: 1.15rem 0 0.45rem;
}

#help h2:first-of-type {
  margin-top: 0.2rem;
}

#help p {
  font-size: 1.08rem;
  line-height: 1.45;
  margin: 0 0 0.65rem;
}

#help-close {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  width: 40px;
  height: 40px;
  border: 3px solid var(--ink);
  background: var(--paper);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
}

#timing-btn {
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  padding: 0.55rem 0.9rem;
  border: 3px solid var(--ink);
  background: var(--brass);
  cursor: pointer;
  margin-top: 0.35rem;
}

#timing-btn:hover {
  background: var(--brass-hot);
}

.shake {
  animation: shake 0.38s ease;
}

@keyframes shake {
  25% {
    transform: translateX(-7px) rotate(-1.6deg);
  }
  75% {
    transform: translateX(7px) rotate(0.4deg);
  }
}

#paper-wrap.ok .sheet,
#card-wrap.ok .sheet {
  box-shadow: 0 0 0 3px var(--brass), 5px 6px 0 rgba(0, 0, 0, 0.22);
}

@media (max-height: 700px) {
  #chart {
    height: 50%;
  }
}

@media (max-width: 900px) {
  .chart-ch {
    font-size: clamp(11px, 1.8vw, 16px);
  }

  .chart-nums .chart-ch {
    font-size: clamp(10px, 1.5vw, 14px);
  }

  .chart-nums .chart-m {
    transform: scale(0.5);
  }
}
