:root {
  --ink: #0c1418;
  --slate: #1a2a32;
  --ridge: #2b4550;
  --mint: #6fd6b6;
  --sand: #efe6d4;
  --rust: #e2552d;
  --steel: #9eb4bd;
  --chip: #f0c43a;
  --danger: #ff5d5d;
  --glow: rgba(111, 214, 182, 0.35);
}

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

html,
body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(ellipse at 15% 10%, #1d3a44 0%, transparent 45%),
    radial-gradient(ellipse at 90% 90%, #243018 0%, transparent 40%),
    linear-gradient(160deg, #070d10 0%, var(--ink) 50%, #101c22 100%);
  color: var(--sand);
  font-family: "IBM Plex Sans", sans-serif;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: stretch;
  padding: 0;
  overflow: auto;
}

.page {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
}

.panel-help {
  margin: 0.4rem 0 0;
  font-size: 0.85rem;
  opacity: 0.7;
}

.btn {
  appearance: none;
  border: 2px solid rgba(158, 180, 189, 0.45);
  background: rgba(26, 42, 50, 0.92);
  color: var(--sand);
  font: 700 0.95rem "IBM Plex Sans", sans-serif;
  padding: 0.7rem 1.15rem;
  cursor: pointer;
  transition:
    transform 0.12s ease,
    border-color 0.12s ease,
    background 0.12s ease;
}

.btn:hover,
.btn:focus-visible {
  transform: translateY(-1px);
  border-color: var(--mint);
  outline: none;
}

.btn.primary {
  background: linear-gradient(180deg, #2f8f78, #1f6a5a);
  border-color: #7fd6c2;
}

.btn.ghost {
  background: transparent;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* —— Morse Trainer page (mobile-first, canvas card) —— */
.page-morse {
  background:
    radial-gradient(ellipse at 50% 100%, rgba(212, 175, 55, 0.1), transparent 50%),
    linear-gradient(180deg, #07090b 0%, #0c1418 55%, #101018 100%);
}

.morse-page {
  width: min(440px, 100%);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding:
    max(0.55rem, env(safe-area-inset-top))
    max(0.65rem, env(safe-area-inset-right))
    max(0.45rem, env(safe-area-inset-bottom))
    max(0.65rem, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  box-sizing: border-box;
}

.morse-page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.4rem 0.75rem;
  flex: 0 0 auto;
}

.morse-mode-toggle {
  order: 3;
  flex-basis: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid rgba(212, 175, 55, 0.35);
  border-radius: 999px;
  padding: 2px;
}

.morse-mode-btn {
  border: 0;
  border-radius: 999px;
  padding: 0.35rem 0.5rem;
  background: transparent;
  color: var(--sand);
  font: 600 0.8rem "IBM Plex Sans", sans-serif;
  letter-spacing: 0.06em;
  cursor: pointer;
}

.morse-mode-btn[aria-pressed="true"] {
  background: linear-gradient(180deg, #5a4a1a, #3a3010);
  color: #ffe9a0;
}

/* Practice and Listen modes hide free-key-only chrome */
.page-morse.is-practice .morse-label,
.page-morse.is-practice .morse-message,
.page-morse.is-practice #btn-morse-record,
.page-morse.is-practice #btn-morse-play,
.page-morse.is-practice #btn-morse-clear,
.page-morse.is-listen .morse-label,
.page-morse.is-listen .morse-message,
.page-morse.is-listen #btn-morse-record,
.page-morse.is-listen #btn-morse-play,
.page-morse.is-listen #btn-morse-clear,
.page-morse.is-listen .morse-controls-primary,
.page-morse.is-listen .morse-help {
  display: none;
}

.page-morse.is-practice .morse-controls-secondary,
.page-morse.is-listen .morse-controls-secondary {
  grid-template-columns: auto;
  justify-content: end;
}

.morse-practice[hidden],
.morse-practice [hidden],
.morse-listen[hidden],
.morse-listen [hidden] {
  display: none !important;
}

/* —— Morse practice (Monkeytype-style send drills) —— */
.morse-practice,
.morse-listen {
  --mp-muted: rgba(214, 199, 161, 0.38);
  --mp-text: #f6f1e1;
  --mp-error: #ff5a4a;
  --mp-accent: #ffd45a;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 0 0 auto;
  font-family: "IBM Plex Sans", sans-serif;
}

.mp-config {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem 0.6rem;
  padding: 0.3rem 0.4rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.28);
  transition: opacity 0.25s ease;
}

.morse-practice.is-running .mp-config {
  opacity: 0.25;
  pointer-events: none;
}

.mp-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.1rem;
}

.mp-group-label {
  font-size: 0.66rem;
  color: var(--mp-muted);
  margin-right: 0.15rem;
}

.mp-pill {
  border: 0;
  background: transparent;
  color: var(--mp-muted);
  font: 600 0.72rem "IBM Plex Sans", sans-serif;
  padding: 0.18rem 0.32rem;
  border-radius: 4px;
  cursor: pointer;
}

.mp-pill:hover,
.mp-pill:focus-visible {
  color: var(--mp-text);
}

.mp-pill[aria-pressed="true"] {
  color: var(--mp-accent);
}

.mp-live {
  margin: 0;
  min-height: 1.1rem;
  text-align: left;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--mp-accent);
}

.mp-test {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.mp-gloss {
  margin: 0;
  font-size: 0.78rem;
  font-style: italic;
  color: rgba(214, 199, 161, 0.7);
}

.mp-words {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem 0.85rem;
  min-height: 2.4rem;
  font-size: clamp(1.35rem, 6vw, 1.7rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.5;
}

.mp-word {
  position: relative;
  display: inline-flex;
  align-items: baseline;
}

.mp-word.is-errored {
  text-decoration: underline;
  text-decoration-color: rgba(255, 90, 74, 0.6);
  text-underline-offset: 0.3em;
}

.ch {
  position: relative;
  transition: color 0.08s linear;
}

.ch-pending {
  color: var(--mp-muted);
}

.ch-correct {
  color: var(--mp-text);
}

.ch-wrong {
  color: var(--mp-error);
}

.ch-extra {
  color: rgba(255, 90, 74, 0.65);
}

.ch-missed {
  color: var(--mp-muted);
  text-decoration: underline;
  text-decoration-color: var(--mp-error);
  text-underline-offset: 0.3em;
}

.ch-prosign {
  text-decoration: overline;
  text-decoration-thickness: 2px;
}

.ch-missed.ch-prosign {
  text-decoration: overline underline;
}

.ch-typo {
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translate(-50%, -0.2em);
  font-size: 0.48em;
  letter-spacing: 0;
  color: var(--mp-error);
  opacity: 0.9;
}

.mp-caret {
  display: inline-block;
  width: 2px;
  height: 1.1em;
  margin: 0 1px;
  align-self: center;
  background: var(--mp-accent);
  animation: mp-caret-blink 1s step-end infinite;
}

.morse-practice.is-running .mp-caret {
  animation: none;
}

@keyframes mp-caret-blink {
  50% {
    opacity: 0;
  }
}

.mp-next {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: rgba(140, 220, 255, 0.9);
  white-space: pre;
}

.mp-upnext {
  margin: 0;
  min-height: 1rem;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  color: var(--mp-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mp-results {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.mp-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.3rem 0.6rem;
}

.mp-stat {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.mp-stat-big {
  grid-column: span 2;
}

.mp-stat-label {
  font-size: 0.7rem;
  color: var(--mp-muted);
}

.mp-stat-value {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--mp-accent);
  white-space: nowrap;
}

.mp-stat-big .mp-stat-value {
  font-size: 2rem;
  line-height: 1.1;
}

.mp-chart {
  display: block;
  width: 100%;
  height: 110px;
}

.mp-misses-title {
  margin: 0 0 0.2rem;
  font-size: 0.7rem;
  color: var(--mp-muted);
}

.mp-miss-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.mp-miss {
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
  padding: 0.15rem 0.45rem;
  border: 1px solid rgba(255, 90, 74, 0.35);
  border-radius: 4px;
  font-size: 0.78rem;
}

.mp-miss-char {
  font-weight: 700;
  color: var(--mp-text);
}

.mp-miss-pattern {
  color: rgba(140, 220, 255, 0.85);
  letter-spacing: 0.08em;
}

.mp-miss-count {
  color: var(--mp-error);
}

.mp-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.mp-restart {
  padding: 0.35rem 0.9rem;
  font-size: 0.8rem;
}

.mp-restart-hint {
  font-size: 0.7rem;
  color: var(--mp-muted);
}

.mp-restart-hint kbd,
.ml-help kbd {
  padding: 0 0.3rem;
  border: 1px solid rgba(212, 175, 55, 0.4);
  border-radius: 3px;
  font-family: inherit;
  color: #d4af37;
}

/* —— Listen and watch —— */
.mp-actions .mp-restart-hint {
  margin-left: auto;
}

.mp-listen,
.mp-listen-toggle {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

.mp-listen.is-active,
.ml-play.is-active,
.ml-loop[aria-pressed="true"] {
  border-color: #d4af37;
  background: linear-gradient(180deg, #5a4a1a, #3a3010);
  color: #ffe9a0;
  box-shadow: 0 0 12px rgba(212, 175, 55, 0.35);
}

.ch-playing {
  color: var(--mp-accent);
  text-shadow: 0 0 10px rgba(255, 212, 90, 0.75);
}

.morse-practice.is-listening .mp-caret {
  visibility: hidden;
}

.ml-phrase {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 1rem;
  min-height: 3.2rem;
}

.ml-word {
  display: inline-flex;
  gap: 0.35rem;
}

.ml-cell {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  min-width: 1.1em;
  transition: color 0.06s linear;
}

.ml-char {
  font-size: clamp(1.25rem, 5.5vw, 1.55rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--mp-muted);
}

.ml-pattern {
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  color: rgba(140, 220, 255, 0.55);
  white-space: nowrap;
}

.ml-cell.is-heard .ml-char {
  color: var(--mp-text);
}

.ml-cell.is-playing .ml-char {
  color: var(--mp-accent);
  text-shadow: 0 0 10px rgba(255, 212, 90, 0.75);
}

.ml-cell.is-playing .ml-pattern {
  color: rgba(140, 220, 255, 1);
}

.ml-strip {
  display: block;
  width: 100%;
  height: 48px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.28);
}

.ml-info {
  margin: 0;
  font-size: 0.72rem;
  color: var(--mp-muted);
}

.ml-controls {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1.2fr;
  gap: 0.4rem;
}

.ml-controls .btn {
  width: 100%;
  min-width: 0;
  padding: 0.55rem 0.4rem;
  font-size: 0.88rem;
}

.ml-help {
  margin: 0;
  text-align: center;
  font-size: 0.7rem;
  color: var(--mp-muted);
}

.morse-page-title {
  margin: 0;
  font-family: Bungee, cursive;
  font-size: clamp(1.1rem, 4.5vw, 1.45rem);
  color: #d4af37;
  letter-spacing: 0.04em;
}

/* —— Wild Idea Zone links —— */
.morse-brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.wiz-home {
  display: flex;
  flex: 0 0 auto;
  border-radius: 7px;
  transition: transform 0.12s ease;
}

.wiz-home img {
  display: block;
  width: 28px;
  height: 28px;
}

.wiz-home:hover,
.wiz-home:focus-visible {
  transform: translateY(-1px) scale(1.06);
  outline: 2px solid rgba(212, 175, 55, 0.6);
  outline-offset: 2px;
}

.wiz-footer {
  grid-column: 1 / -1;
  margin: 0.6rem 0 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--steel);
  opacity: 0.8;
}

.wiz-footer a {
  color: #d4af37;
  text-decoration: none;
}

.wiz-footer a:hover,
.wiz-footer a:focus-visible {
  text-decoration: underline;
  outline: none;
}

.morse-dock {
  flex: 1 1 auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4rem;
  min-height: 0;
  margin-top: auto;
  padding-bottom: 0.15rem;
}

.morse-board {
  width: 100%;
  max-width: 440px;
  display: flex;
  justify-content: center;
  flex: 0 1 auto;
  min-height: 0;
}

.morse-board-canvas {
  display: block;
  width: 100%;
  height: auto;
  max-width: 100%;
  touch-action: none;
  filter: drop-shadow(0 10px 28px rgba(0, 0, 0, 0.55));
  border-radius: 18px;
}

.morse-readout {
  width: 100%;
  text-align: center;
  flex: 0 0 auto;
}

.morse-label {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #d4af37;
  opacity: 0.8;
}

.morse-message {
  margin: 0.05rem 0 0;
  min-height: 1.2rem;
  font-family: "IBM Plex Sans", sans-serif;
  font-weight: 700;
  font-size: clamp(1.15rem, 4vw, 1.5rem);
  letter-spacing: 0.14em;
  color: #f5e6a8;
  word-break: break-word;
}

.morse-status {
  margin: 0.05rem 0 0;
  font-size: 0.75rem;
  opacity: 0.7;
  color: var(--sand);
}

.morse-controls {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem;
  flex: 0 0 auto;
}

.morse-controls-secondary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.35rem;
}

.morse-controls-secondary .btn {
  width: 100%;
  min-width: 0;
  padding: 0.55rem 0.35rem;
  font-size: 0.82rem;
}

.morse-controls-primary {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1fr;
  gap: 0.45rem;
  width: 100%;
}

.morse-controls-primary .btn {
  width: 100%;
  min-height: 3.25rem;
  font-size: 1.05rem;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.morse-paddle.is-down,
.morse-controls .btn.is-active {
  border-color: #d4af37;
  background: linear-gradient(180deg, #5a4a1a, #3a3010);
  color: #ffe9a0;
  box-shadow: 0 0 12px rgba(212, 175, 55, 0.35);
}

.morse-help {
  margin: 0;
  text-align: center;
  font-size: 0.78rem;
}

.morse-help kbd {
  display: inline-block;
  padding: 0.05rem 0.35rem;
  border: 1px solid rgba(212, 175, 55, 0.4);
  border-radius: 3px;
  font-family: "IBM Plex Sans", sans-serif;
  font-size: 0.78em;
  color: #d4af37;
}

@media (max-width: 640px) {
  .morse-controls-primary .btn {
    min-height: 3.6rem;
  }
}

@media (min-width: 720px) {
  .morse-page {
    padding-top: 1rem;
    padding-bottom: 1rem;
  }
}

/* —— Morse page: panel wrapper + code chart (compact defaults) —— */
.morse-panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  flex: 0 0 auto;
}

.morse-reference {
  display: none;
}

/* —— Laptop: card column + mode panel —— */
@media (min-width: 900px) and (min-height: 560px) {
  .morse-page {
    width: min(1200px, 100%);
    display: grid;
    grid-template-columns: auto minmax(380px, 1fr);
    grid-template-areas:
      "header header"
      "dock panel";
    align-items: start;
    column-gap: 1.75rem;
    row-gap: 0.75rem;
    padding: 1rem 1.5rem;
  }

  .morse-page-header {
    grid-area: header;
    align-items: center;
    flex-wrap: nowrap;
  }

  .morse-mode-toggle {
    order: 0;
    flex-basis: auto;
    width: 360px;
  }

  .morse-dock {
    grid-area: dock;
    width: min(46vw, 62vh, 740px);
    margin-top: 0;
    justify-content: flex-start;
    position: sticky;
    top: 1rem;
  }

  .morse-board {
    max-width: none;
  }

  .morse-panel {
    grid-area: panel;
    gap: 0.75rem;
    padding-top: 0.25rem;
  }

  .page-morse:not(.is-practice):not(.is-listen) .morse-readout {
    min-height: 9rem;
    padding: 1.25rem 1rem;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.28);
  }

  .morse-label {
    font-size: 0.8rem;
  }

  .morse-message {
    font-size: clamp(1.4rem, 2.2vw, 2.2rem);
  }

  .morse-status {
    font-size: 0.85rem;
  }

  .mp-config {
    padding: 0.45rem 0.6rem;
  }

  .mp-pill {
    font-size: 0.8rem;
  }

  .mp-live {
    font-size: 1.15rem;
  }

  .mp-gloss {
    font-size: 0.9rem;
  }

  .mp-words {
    font-size: clamp(1.6rem, 2.4vw, 2.4rem);
    min-height: 3.4rem;
  }

  .mp-next {
    font-size: 1.05rem;
  }

  .mp-upnext {
    font-size: 0.8rem;
  }

  .mp-chart {
    height: 160px;
  }

  .ml-char {
    font-size: clamp(1.4rem, 2vw, 2rem);
  }

  .ml-pattern {
    font-size: 0.8rem;
  }

  .ml-strip {
    height: 72px;
  }

  .ml-info,
  .ml-help {
    font-size: 0.8rem;
  }
}

/* —— Wide: add the live code chart column —— */
@media (min-width: 1440px) and (min-height: 560px) {
  .morse-page {
    width: min(1640px, 100%);
    grid-template-columns: auto minmax(420px, 1fr) 300px;
    grid-template-areas:
      "header header header"
      "dock panel ref";
  }

  .morse-reference {
    grid-area: ref;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
    max-height: calc(100dvh - 2rem);
    overflow-y: auto;
    padding: 0.75rem;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.28);
    font-family: "IBM Plex Sans", sans-serif;
  }
}

.ref-title {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #d4af37;
  opacity: 0.85;
}

.ref-hint {
  margin: 0;
  font-size: 0.7rem;
  color: rgba(214, 199, 161, 0.5);
}

.ref-player {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.55rem 0.6rem;
  border: 1px solid rgba(214, 199, 161, 0.14);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  transition: border-color 0.15s ease;
}

.ref-player.is-active {
  border-color: rgba(212, 175, 55, 0.7);
}

.ref-player-head {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-height: 1.6rem;
}

.ref-player-head .ref-char {
  min-width: 0;
  font-size: 1.4rem;
  color: #ffd45a;
}

.ref-player-head .ref-glyphs {
  gap: 4px;
}

.ref-player-head .ref-dit,
.ref-player-head .ref-dah {
  height: 8px;
  border-radius: 4px;
}

.ref-player-head .ref-dit {
  width: 8px;
}

.ref-player-head .ref-dah {
  width: 24px;
}

.ref-strip {
  display: block;
  width: 100%;
  height: 56px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.28);
}

.ref-strip[hidden],
.ref-player-meta[hidden] {
  display: none !important;
}

.ref-player-meta {
  margin: 0;
  font-size: 0.68rem;
  color: rgba(214, 199, 161, 0.6);
}

.ref-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.1rem 0.6rem;
}

.ref-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  width: 100%;
  padding: 0.18rem 0.35rem;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: #f6f1e1;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.ref-row:hover,
.ref-row:focus-visible {
  background: rgba(214, 199, 161, 0.08);
}

.ref-char {
  min-width: 1.6em;
  font-size: 0.95rem;
  font-weight: 700;
}

.ref-glyphs {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.ref-dit,
.ref-dah {
  display: inline-block;
  height: 6px;
  border-radius: 3px;
}

.ref-dit {
  width: 6px;
  background: #3dcc6e;
}

.ref-dah {
  width: 18px;
  background: #e05544;
}

.ref-row.is-expected {
  border-color: rgba(140, 220, 255, 0.7);
}

.ref-row.is-keyed,
.ref-row.is-playing {
  border-color: #d4af37;
  background: rgba(255, 212, 90, 0.14);
  box-shadow: 0 0 10px rgba(255, 212, 90, 0.25);
}

.ref-row.is-keyed .ref-char,
.ref-row.is-playing .ref-char {
  color: #ffd45a;
}

.ref-row.is-keyed .ref-dit,
.ref-row.is-playing .ref-dit {
  background: #5dff96;
}

.ref-row.is-keyed .ref-dah,
.ref-row.is-playing .ref-dah {
  background: #ff6a52;
}
