@import url("./tokens.css");

:root {
  --chord-body-h: 10.75rem;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  overflow-x: clip;
}

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

body {
  margin: 0;
  min-height: 100dvh;
  color: var(--color-ink);
  background: var(--color-paper);
  font-family: var(--font-body);
  font-weight: 400;
  font-size: var(--text-md);
  line-height: 1.5;
}

button,
select {
  font: inherit;
}

a {
  color: inherit;
}

.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;
}

.nav-edge,
.stage {
  width: min(100%, 86rem);
  margin-inline: auto;
}

.nav-edge {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: var(--space-lg);
  padding: var(--space-lg) var(--page-gutter) var(--space-md);
}

.wordmark {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  min-width: 0;
  padding-bottom: 0.2rem;
  text-decoration: none;
}

.logo {
  width: 1.55rem;
  height: 1.55rem;
  flex: 0 0 auto;
  fill: var(--color-ink);
}

.wordmark-name {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 600;
  font-style: normal;
  letter-spacing: -0.045em;
  overflow-wrap: anywhere;
}

.wordmark-rule {
  width: 1px;
  height: 1rem;
  background: var(--color-rule-2);
}

.wordmark-meta {
  color: var(--color-neutral);
  font-family: var(--font-outlier);
  font-size: var(--text-xs);
  letter-spacing: 0.04em;
}

.nav-switch {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2xs);
  margin-right: auto;
  padding-bottom: 0.15rem;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: 2rem;
  padding: 0 0.7rem;
  border: 1px solid transparent;
  border-radius: 999px;
  color: var(--color-muted);
  font-size: var(--text-sm);
  text-decoration: none;
  white-space: nowrap;
}

.nav-link:hover {
  color: var(--color-ink);
}

.nav-link.is-current {
  border-color: var(--color-rule);
  background: var(--color-paper-2);
  color: var(--color-ink);
}

.nav-link:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: flex-end;
  gap: var(--space-xs);
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-xs);
}

.is-mobile {
  display: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-width: 0;
}

.field label {
  color: var(--color-neutral);
  font-size: 0.7rem;
  letter-spacing: 0.01em;
}

.field select,
.btn {
  appearance: none;
  min-height: var(--control-h);
  border: 1px solid var(--color-rule);
  border-radius: 999px;
  background: var(--color-paper-2);
  color: var(--color-ink);
  touch-action: manipulation;
}

.field select {
  min-width: 11.5rem;
  max-width: 16rem;
  padding: 0 2rem 0 0.95rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%239aa3b2' d='M1.2 1.2 6 6l4.8-4.8'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.85rem center;
  cursor: pointer;
}

.field select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.field select:focus-visible,
.btn:focus-visible,
.btn.is-focus {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0 1rem;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background-color var(--dur-micro) var(--ease-out),
    transform var(--dur-micro) var(--ease-out),
    opacity var(--dur-micro) var(--ease-out);
}

.btn:hover,
.btn.is-hover {
  background: var(--color-paper-3);
}

.btn:active,
.btn.is-active {
  transform: translateY(1px);
}

.btn:disabled,
.btn.is-disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-dot {
  display: none;
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--color-ok);
  box-shadow: 0 0 0 3px oklch(78% 0.16 150 / 0.16);
}

.btn-connect[data-state="success"] .btn-dot {
  display: block;
}

.btn-connect[data-state="loading"] #connect-label::after {
  content: "";
  display: inline-block;
  width: 0.65rem;
  height: 0.65rem;
  margin-left: 0.4rem;
  border: 2px solid oklch(90% 0.01 250 / 0.2);
  border-top-color: var(--color-ink);
  border-radius: 50%;
  vertical-align: -0.1rem;
  animation: spin 0.7s linear infinite;
}

.btn-connect[data-state="error"] {
  border-color: var(--color-danger);
  color: var(--color-danger);
}

.stage {
  padding: var(--space-sm) var(--page-gutter) var(--space-2xl);
}

.status-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  min-height: 2.25rem;
  margin-bottom: var(--space-lg);
}

.status-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem 0.7rem;
  margin: 0;
  color: var(--color-ink);
  font-size: var(--text-sm);
}

.status-dot {
  display: none;
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--color-ok);
  box-shadow: 0 0 0 3px oklch(78% 0.16 150 / 0.16);
}

.status-line[data-kind="ok"] .status-dot {
  display: inline-block;
}

.status-line[data-kind="error"] {
  color: var(--color-danger);
}

.status-line[data-kind="error"] .status-dot {
  display: inline-block;
  background: var(--color-danger);
  box-shadow: 0 0 0 3px oklch(68% 0.18 25 / 0.16);
}

.status-hint {
  color: var(--color-neutral);
}

.live-notes {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2xs);
  min-width: 0;
  min-height: 1.45rem;
  max-height: 1.45rem;
  overflow-x: auto;
  overflow-y: hidden;
}

.note-chip {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--color-paper-3);
  color: var(--color-ink);
  font-family: var(--font-outlier);
  font-size: var(--text-xs);
  letter-spacing: 0.02em;
}

.card-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.7fr);
  align-items: stretch;
  gap: var(--space-md);
  margin-bottom: var(--space-md);
}

.panel-card,
.piano-card {
  padding: var(--space-md) var(--space-md) var(--space-sm);
  border: 1px solid var(--color-rule);
  border-radius: var(--radius-lg);
  background: var(--color-paper-2);
  min-width: 0;
}

.staff-card,
.chord-card {
  display: flex;
  flex-direction: column;
}

.staff-card {
  min-height: 0;
}

.chord-card {
  min-height: max-content;
  overflow: hidden;
}

.staff-card .piano-card-head,
.chord-card .piano-card-head {
  margin-bottom: var(--space-xs);
}

.chord-card .piano-card-head {
  flex-wrap: nowrap;
}

.chord-card .piano-card-meta {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.staff-key-field {
  margin-left: auto;
}

.staff-key-field select {
  min-width: 11.5rem;
  max-width: 16rem;
}

.staff-svg {
  display: block;
  flex: 1 1 auto;
  width: 100%;
  height: auto;
}

.staff-line,
.staff-bar,
.staff-ledger,
.staff-stem,
.staff-accidental path {
  stroke: var(--color-muted);
  stroke-linecap: round;
  stroke-linejoin: round;
}

.staff-line {
  stroke-width: 1.1;
}

.staff-bar,
.staff-ledger {
  stroke-width: 1.35;
}

.staff-stem {
  stroke: var(--color-accent);
  stroke-width: 1.4;
}

.staff-brace,
.staff-clef {
  fill: var(--color-muted);
}

.staff-clef path {
  fill: var(--color-muted);
}

.staff-accidental {
  fill: var(--color-muted);
}

.staff-accidental path {
  fill: var(--color-muted);
}

.staff-accidental path[fill="none"] {
  fill: none;
}

.staff-zone,
.staff-empty {
  fill: var(--color-neutral);
  font-family: var(--font-body);
  font-size: 11px;
}

.staff-empty {
  font-size: 13px;
}

.staff-head {
  fill: var(--color-accent);
}

.chord-body {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: var(--chord-body-h);
  overflow: hidden;
}

.chord-empty {
  display: grid;
  flex: 1 1 auto;
  align-content: center;
  justify-items: center;
  gap: var(--space-2xs);
  min-height: var(--chord-body-h);
  height: 100%;
  overflow: hidden;
  padding: var(--space-md);
  border: 1px dashed var(--color-rule-2);
  border-radius: var(--radius-md);
  color: var(--color-neutral);
  text-align: center;
}

.chord-empty strong {
  color: var(--color-muted);
  font-weight: 500;
  font-size: var(--text-sm);
}

.chord-empty span {
  max-width: 16rem;
  font-size: var(--text-xs);
  line-height: 1.45;
}

.chord-hit {
  display: grid;
  flex: 1 1 auto;
  grid-template-rows: 2.9rem 1.3rem 3.4rem 1.55rem;
  align-content: center;
  gap: var(--space-xs);
  min-height: var(--chord-body-h);
  height: 100%;
  overflow: hidden;
  padding: 0 0.15rem;
}

.chord-symbol,
.chord-quality {
  margin: 0;
}

.chord-symbol {
  display: flex;
  align-items: center;
  min-height: 0;
  overflow: hidden;
  color: var(--color-ink);
  font-family: var(--font-display);
  font-size: clamp(1.7rem, 3.5vw, 2.55rem);
  font-weight: 600;
  font-style: normal;
  letter-spacing: -0.05em;
  line-height: 1;
  overflow-wrap: anywhere;
}

.chord-symbol-acc {
  color: var(--color-accent);
}

.chord-quality {
  min-height: 1.3rem;
  overflow: hidden;
  color: var(--color-muted);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chord-degrees {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2xs);
  height: 3.4rem;
  min-height: 3.4rem;
}

.chord-degrees[data-empty="true"] {
  visibility: hidden;
}

.chord-degree {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  height: 100%;
  padding: 0.28rem 0.5rem;
  border: 1px solid var(--color-rule);
  border-radius: 10px;
  background: var(--color-paper);
}

.chord-degree-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.35rem;
  min-width: 0;
}

.chord-degree-numeral {
  overflow: hidden;
  color: var(--color-accent);
  font-family: var(--font-outlier);
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: 0.01em;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chord-degree-key {
  overflow: hidden;
  color: var(--color-muted);
  font-size: var(--text-xs);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chord-degree-alt {
  flex: 0 0 auto;
  color: color-mix(in srgb, var(--color-muted) 78%, var(--color-ink));
  font-family: var(--font-outlier);
  font-size: 0.72rem;
}

.chord-tones {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: var(--space-2xs);
  min-height: 1.55rem;
  overflow: hidden;
}

.piano-card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-xs) var(--space-sm);
  margin-bottom: var(--space-md);
  color: var(--color-ink);
  font-size: var(--text-sm);
}

.piano-card-head svg {
  flex: 0 0 auto;
  width: 1.05rem;
  height: 1.05rem;
  color: var(--color-muted);
}

.piano-card-title {
  font-weight: 500;
}

.piano-card-meta {
  color: var(--color-neutral);
}

.piano-viewport {
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
}

.piano-viewport:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 4px;
}

.piano-inner {
  position: relative;
  min-width: max(100%, 48rem);
}

.piano {
  position: relative;
  height: var(--piano-h);
  user-select: none;
  touch-action: none;
}

#white-keys {
  display: flex;
  width: 100%;
  height: 100%;
}

#black-keys {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.white-key {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  border: 0;
  border-right: 1px solid var(--color-key-white-edge);
  border-radius: 0 0 0.2rem 0.2rem;
  background: var(--color-key-white);
  padding: 0;
  cursor: pointer;
}

.white-key:last-child {
  border-right: 0;
}

.white-key.is-on {
  background: var(--color-key-white-on);
}

.black-key {
  position: absolute;
  top: 0;
  z-index: 2;
  width: calc(100% / 52 * 0.58);
  height: var(--black-key-h);
  padding: 0;
  border: 0;
  border-radius: 0 0 0.16rem 0.16rem;
  background: var(--color-key-black);
  transform: translateX(-50%);
  pointer-events: auto;
  cursor: pointer;
}

.white-key:focus-visible,
.black-key:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: -2px;
  z-index: 3;
}

.black-key.is-on {
  background: var(--color-key-black-on);
}

.octave-rail {
  position: relative;
  height: 1.5rem;
  margin-top: 0.35rem;
}

.octave-mark {
  position: absolute;
  transform: translateX(-50%);
  color: var(--color-neutral);
  font-family: var(--font-outlier);
  font-size: 0.68rem;
  letter-spacing: 0.06em;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (hover: none) {
  .btn:hover {
    background: var(--color-paper-2);
  }
}

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

@media (max-width: 48rem), (max-width: 64rem) and (max-height: 32rem) {
  :root {
    --page-gutter: 1rem;
    --control-h: 2.75rem;
    --piano-h: 7.25rem;
    --black-key-h: 64%;
  }

  html,
  body {
    height: 100dvh;
    overflow: hidden;
  }

  body {
    display: flex;
    flex-direction: column;
  }

  .nav-edge,
  .stage {
    width: 100%;
    padding-left: max(var(--page-gutter), env(safe-area-inset-left));
    padding-right: max(var(--page-gutter), env(safe-area-inset-right));
  }

  .nav-edge {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    flex: 0 0 auto;
    align-items: center;
    gap: var(--space-xs);
    padding-top: max(var(--space-sm), env(safe-area-inset-top));
    padding-bottom: var(--space-sm);
  }

  .wordmark {
    padding-bottom: 0;
  }

  .wordmark-name {
    font-size: 1.15rem;
  }

  .wordmark-rule,
  .wordmark-meta {
    display: none;
  }

  .nav-switch {
    margin-right: 0;
    padding-bottom: 0;
  }

  .nav-link {
    min-height: var(--control-h);
  }

  .toolbar {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: minmax(0, 1fr);
    justify-content: stretch;
    gap: var(--space-xs);
  }

  .toolbar .field {
    gap: 0.15rem;
  }

  .toolbar .field,
  .toolbar .field select {
    width: 100%;
    min-width: 0;
    max-width: none;
  }

  .toolbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .field select {
    font-size: 1rem;
  }

  .stage {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    padding-top: var(--space-xs);
    padding-bottom: 0;
  }

  .status-row {
    flex: 0 0 auto;
    align-items: flex-start;
    margin-bottom: var(--space-sm);
  }

  .status-line {
    font-size: var(--text-xs);
  }

  .live-notes {
    justify-content: flex-start;
    width: 100%;
  }

  .card-row {
    grid-template-columns: minmax(0, 1fr);
    flex: 1 1 auto;
    align-content: start;
    gap: var(--space-sm);
    min-height: 0;
    margin-bottom: var(--space-sm);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .staff-card {
    min-height: auto;
  }

  .staff-svg {
    flex: 0 0 auto;
    height: auto;
  }

  .staff-card .piano-card-head {
    gap: var(--space-xs);
  }

  .staff-key-field {
    flex: 1 1 100%;
    margin-left: 0;
  }

  .staff-key-field,
  .staff-key-field select {
    width: 100%;
    max-width: none;
  }

  .chord-card {
    flex: 0 0 auto;
    min-height: max-content;
  }

  .chord-body,
  .chord-empty,
  .chord-hit {
    flex: 0 0 var(--chord-body-h);
    height: var(--chord-body-h);
    min-height: var(--chord-body-h);
  }

  .chord-symbol {
    font-size: clamp(1.6rem, 8vw, 2.2rem);
  }

  .is-desktop {
    display: none;
  }

  .is-mobile {
    display: inline;
  }

  .piano-card {
    flex: 0 0 auto;
    z-index: 5;
    margin-left: calc(-1 * max(var(--page-gutter), env(safe-area-inset-left)));
    margin-right: calc(-1 * max(var(--page-gutter), env(safe-area-inset-right)));
    padding-bottom: max(var(--space-sm), env(safe-area-inset-bottom));
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -12px 28px oklch(0% 0 0 / 0.32);
  }

  .piano-card-head {
    margin-bottom: var(--space-xs);
  }

  .piano-inner {
    min-width: max(100%, calc(52 * 2.05rem));
  }
}

@media (max-width: 64rem) and (max-height: 32rem) {
  :root {
    --piano-h: 5.4rem;
  }

  .nav-edge {
    grid-template-columns: auto auto minmax(0, 1fr);
    gap: var(--space-xs);
    padding-top: max(0.35rem, env(safe-area-inset-top));
    padding-bottom: 0.35rem;
  }

  .toolbar {
    grid-column: auto;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: var(--space-2xs);
  }

  .toolbar .field label {
    display: none;
  }

  .toolbar-actions {
    display: flex;
    flex-wrap: nowrap;
  }

  .status-hint {
    display: none;
  }

  .staff-svg {
    width: min(100%, calc(28vh * 760 / 436));
    max-height: 28vh;
  }

  .piano-card-head {
    display: none;
  }
}
