:root {
  --bg: #121213;
  --surface: #1e1e20;
  --surface-2: #2a2a2d;
  --text: #f2f2f2;
  --muted: #a1a1a6;
  --border: #3a3a3c;
  --accent: #7c6cf2;
  --accent-text: #ffffff;
  --correct: #538d4e;
  --close: #b59f3b;
  --wrong: #3a3a3c;
  --danger: #e0617d;
  --key-white: #f7f7f2;
  --key-black: #1b1b1b;
  --first: #7c6cf2;
  --second: #e0617d;
  --compare: #4fa3d9;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --surface: #f3f3f5;
    --surface-2: #e6e6ea;
    --text: #1a1a1b;
    --muted: #6b6b70;
    --border: #d3d6da;
    --accent: #5b4bd8;
    --wrong: #787c7e;
    --danger: #c23b5a;
    color-scheme: light;
  }
}

/* Colors that stay distinguishable with color vision deficiency (as in Wordle). */
:root.high-contrast {
  --correct: #f5793a;
  --close: #85c0f9;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.4;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: default;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

a {
  color: var(--accent);
}

/* Header */

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.1rem, 5.2vw, 1.9rem);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.topbar-side {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-side.end {
  justify-content: flex-end;
}

.icon-button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: none;
  font-weight: 700;
  font-size: 1.1rem;
  display: grid;
  place-items: center;
  padding: 0;
}

.flag-button svg {
  width: 24px;
  height: auto;
  border-radius: 3px;
  display: block;
}

.language-menu {
  position: relative;
}

.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 20;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  min-width: 200px;
}

.menu button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: none;
  border-radius: 6px;
  background: none;
  text-align: left;
}

.menu button:hover,
.menu button[aria-checked='true'] {
  background: var(--surface-2);
}

.menu svg {
  width: 26px;
  height: auto;
  border-radius: 3px;
  flex: none;
}

/* Game */

.game {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.switches {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.segmented {
  display: flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px;
}

.segmented button {
  border: none;
  background: none;
  padding: 6px 16px;
  border-radius: 999px;
  font-weight: 700;
  color: var(--muted);
}

.segmented button[aria-selected='true'] {
  background: var(--bg);
  color: var(--text);
}

.subtitle {
  margin: 0;
  text-align: center;
  color: var(--muted);
}

.subtitle span:first-child {
  color: var(--text);
  font-weight: 700;
}

.practice-stats,
.archive-note {
  margin: -12px 0 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
}

.link-button {
  border: none;
  background: none;
  padding: 0;
  color: var(--accent);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.link-button.danger {
  color: var(--danger);
}

/* Practice settings */

.practice-panel {
  background: var(--surface);
  border-radius: 12px;
  padding: 10px 14px;
}

.practice-panel summary {
  cursor: pointer;
  font-weight: 700;
}

.practice-panel fieldset {
  border: none;
  margin: 12px 0 0;
  padding: 0;
}

.practice-panel legend,
.practice-panel h4 {
  font-weight: 700;
  font-size: 0.9rem;
  margin: 14px 0 6px;
  padding: 0;
}

.group-label {
  margin: 8px 0 6px;
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  gap: 8px;
  align-items: baseline;
}

.chips,
.choice-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.85rem;
  cursor: pointer;
  border: 2px solid transparent;
}

.chip:has(input:checked) {
  border-color: var(--accent);
}

.chip input {
  margin: 0;
  accent-color: var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  font-size: 0.9rem;
}

.toggle-row input {
  accent-color: var(--accent);
}

.weak-spots {
  margin: 0 0 10px;
  padding-left: 18px;
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Listening */

.listen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.listen-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.listens-left {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.play-button {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 14px 22px;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  font-size: 1.05rem;
  transition: transform 0.1s, opacity 0.2s;
}

.play-button:active:not(:disabled) {
  transform: scale(0.96);
}

.play-button.secondary {
  background: var(--surface-2);
  color: var(--text);
}

.play-button:disabled {
  opacity: 0.4;
}

.play-button.playing .play-icon {
  animation: pulse 0.6s ease-in-out infinite alternate;
}

@keyframes pulse {
  to {
    transform: scale(1.35);
  }
}

.attempts {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.attempt {
  min-height: 52px;
  border: 2px solid var(--border);
  border-radius: 8px;
  display: grid;
  place-items: center;
  text-align: center;
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1.15;
  padding: 2px 4px;
}

.attempt.correct,
.attempt.close,
.attempt.wrong {
  border-color: transparent;
  color: #fff;
}

.attempt.reveal {
  animation: flip 0.4s ease;
}

.correct {
  background: var(--correct);
}

.close {
  background: var(--close);
}

.wrong {
  background: var(--wrong);
}

@keyframes flip {
  from {
    transform: rotateX(90deg);
  }
}

.hints {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.hints:empty {
  display: none;
}

.hints li {
  background: var(--surface);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 0.95rem;
}

.hints li::before {
  content: '💡 ';
}

/* Answer choices */

.choices {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.option-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.option {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 4px;
  border-radius: 8px;
  border: 2px solid transparent;
  background: var(--surface-2);
  text-align: center;
  line-height: 1.2;
  transition: transform 0.1s;
}

.option small {
  color: var(--muted);
  font-size: 0.75rem;
}

.option:active:not(:disabled) {
  transform: scale(0.96);
}

.option.selected {
  border-color: var(--accent);
}

.option.correct {
  background: var(--correct);
}

.option.close {
  background: var(--close);
}

.option.wrong {
  background: var(--wrong);
}

.option.correct,
.option.close,
.option.wrong {
  color: #fff;
}

.option.correct small,
.option.close small,
.option.wrong small {
  color: rgba(255, 255, 255, 0.85);
}

.option.answer {
  box-shadow: 0 0 0 3px var(--correct);
}

.option:disabled {
  opacity: 0.75;
}

.confirm-button,
.action-button {
  padding: 14px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  font-size: 1rem;
}

.action-button.secondary {
  background: var(--surface-2);
  color: var(--text);
}

.action-button.wide {
  width: 100%;
  margin-top: 18px;
}

.confirm-button:disabled {
  background: var(--surface-2);
  color: var(--muted);
}

/* Result */

.result {
  background: var(--surface);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.result p {
  margin: 0;
}

.result-title {
  font-size: 1.4rem;
  font-weight: 800;
}

.result-song {
  font-size: 0.95rem;
}

.result-tip {
  color: var(--muted);
  font-size: 0.85rem;
}

.result-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.result-actions .action-button {
  flex: 1;
  background: var(--correct);
}

.countdown {
  flex: 1;
  display: flex;
  flex-direction: column;
  font-size: 0.8rem;
  color: var(--muted);
}

.countdown strong {
  font-size: 1.3rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Keyboard */

.keyboard {
  position: relative;
  display: flex;
  height: 110px;
  border-radius: 6px;
  overflow: hidden;
  background: var(--key-black);
  padding-top: 4px;
}

.key {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font-weight: 800;
  font-size: 0.8rem;
  color: #fff;
}

.key span {
  padding-bottom: 6px;
}

.key.white {
  flex: 1;
  background: var(--key-white);
  border-right: 1px solid #c8c8c0;
  border-radius: 0 0 4px 4px;
}

.key.black {
  position: absolute;
  top: 4px;
  height: 62%;
  background: var(--key-black);
  border-radius: 0 0 3px 3px;
  z-index: 1;
}

.key.first {
  background: var(--first);
}

.key.second {
  background: var(--second);
}

.key.compare {
  background: var(--compare);
}

/* A note in both the I chord and the answer chord. */
.key.shared {
  background: linear-gradient(90deg, var(--first) 50%, var(--second) 50%);
}

.option-grid.harmony strong {
  font-size: 1.3rem;
  font-family: Georgia, 'Times New Roman', serif;
}

/* Modals */

.modal {
  width: min(480px, calc(100% - 32px));
  max-height: calc(100dvh - 32px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  background: var(--bg);
  color: var(--text);
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.modal h2 {
  margin: 0 0 12px;
  padding-right: 32px;
}

.modal h3 {
  margin: 18px 0 8px;
  font-size: 1rem;
}

.modal p {
  margin: 0 0 8px;
}

.close-button {
  position: absolute;
  top: 10px;
  right: 12px;
  border: none;
  background: none;
  font-size: 1.8rem;
  line-height: 1;
}

.legend,
.families {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.swatch {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 4px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
}

.stat strong {
  display: block;
  font-size: 1.8rem;
}

.stat span {
  font-size: 0.75rem;
  color: var(--muted);
}

.distribution {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.bar-row > span {
  width: 12px;
}

.bar {
  background: var(--wrong);
  color: #fff;
  font-weight: 700;
  text-align: right;
  padding: 2px 8px;
  min-width: 28px;
}

.bar.today {
  background: var(--correct);
}

/* Archive */

.archive-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.archive-list button {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: var(--surface-2);
  text-align: left;
}

.archive-day {
  font-weight: 700;
}

.archive-date {
  grid-row: 2;
  color: var(--muted);
  font-size: 0.85rem;
}

.archive-state {
  grid-row: 1 / 3;
  grid-column: 2;
  align-self: center;
  font-size: 0.9rem;
}

.archive-empty {
  color: var(--muted);
  font-size: 0.9rem;
  padding: 6px 2px;
}

/* Settings */

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.setting.column {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.setting label {
  display: flex;
  flex-direction: column;
}

.setting small {
  color: var(--muted);
  font-size: 0.8rem;
}

.switch {
  appearance: none;
  flex: none;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  margin: 0;
  transition: background 0.2s;
}

.switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  transition: transform 0.2s;
}

.switch:checked {
  background: var(--correct);
}

.switch:checked::after {
  transform: translateX(18px);
}

input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
}

.import-row {
  display: flex;
  gap: 8px;
}

.import-row input {
  flex: 1;
  min-width: 0;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.credits {
  margin-top: 14px !important;
  font-size: 0.75rem;
  color: var(--muted);
}

/* Toast */

.toast {
  position: fixed;
  top: calc(72px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 700;
  z-index: 30;
  max-width: calc(100% - 32px);
  text-align: center;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
}

kbd {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 5px;
  font-size: 0.8rem;
}

@media (min-width: 480px) {
  .option-grid.intervals {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 400px) {
  .topbar {
    padding-left: 12px;
    padding-right: 12px;
    gap: 6px;
  }

  .topbar-side {
    gap: 6px;
  }

  .icon-button {
    width: 38px;
    height: 38px;
  }

  .segmented button {
    padding: 6px 12px;
  }
}

/* Respect the system setting for less motion. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
