:root {
  color-scheme: light;
  font-family: 'Instrument Sans', 'Segoe UI', system-ui, sans-serif;
  color: #202a25;
  background: #f8f6ef;
  --paper: #f8f6ef;
  --muted: #555f56;
  --line: #dedfd3;
  --forest: #214638;
  --target: #f0ca71;
  --played: #8dceab;
  --extra: #efa990;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
body {
  margin: 0;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
button { font: inherit; cursor: pointer; }
button:focus-visible, a:focus-visible { outline: 2px solid #98701b; outline-offset: 4px; }
h1, h2, p, ul { margin: 0; }
button, a { -webkit-tap-highlight-color: transparent; }

.masthead, main { width: min(880px, calc(100% - 64px)); margin-inline: auto; }
.masthead {
  min-height: 81px;
  padding-block: max(16px, env(safe-area-inset-top)) 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
}
.brand, .midi, .midi-status { display: flex; align-items: center; }
.brand { gap: 12px; }
.brand-mark {
  width: 44px;
  height: 44px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 3px;
  border-radius: 13px;
  background: var(--forest);
  flex: none;
}
.brand-mark i { width: 5px; height: 20px; border-radius: 1px 1px 2px 2px; background: #f8f6ef; }
.brand-mark i:nth-child(2) { height: 12px; align-self: flex-start; margin-top: 12px; background: var(--target); }
.brand h1 { font-size: 20px; line-height: 1.1; letter-spacing: -.05em; }
.mode-title { font-weight: 730; letter-spacing: -.025em; }
.masthead .midi { margin-inline-start: auto; }
.midi { gap: 6px 12px; flex-wrap: wrap; justify-content: flex-end; }
.midi-status {
  gap: 8px;
  width: max-content;
  padding: 7px 11px;
  border-radius: 40px;
  background: #eeece2;
  color: #4e554b;
  font-size: 12px;
  font-weight: 650;
}
.midi-status.connected { background: #e3efe3; color: #244a37; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #a97920; }
.connected .status-dot { background: #32805a; }
.text-button { background: transparent; border: 0; color: var(--forest); text-decoration: underline; padding: 2px; font-size: 12px; }

main { padding-block: 25px max(26px, env(safe-area-inset-bottom)); }
.home { padding: clamp(22px, 6vh, 66px) 0; }
.home h2 { margin-bottom: 22px; font: 400 clamp(32px, 6vw, 52px)/1.03 'Instrument Serif', Georgia, serif; letter-spacing: -.055em; color: var(--forest); }
.home h2:focus, #rhythm-heading:focus { outline: none; }
.mode-options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.mode-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 13px;
  min-height: 88px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  text-align: left;
  color: var(--forest);
  background: #fffcf3;
}
.mode-card:hover { background: #eaf0e3; border-color: #bacbb9; }
.mode-card strong, .mode-card small { display: block; }
.mode-card strong { font-size: 18px; }
.mode-card small { margin-top: 3px; color: var(--muted); font-size: 12px; }
.mode-icon { display: grid; place-items: center; width: 41px; height: 41px; border-radius: 8px; background: #e6ebde; font-size: 23px; }
.mode-arrow { padding-inline-end: 2px; }
#midi-help { margin-top: 16px; color: var(--muted); font-size: 13px; }
.back-button, .secondary-button { min-height: 38px; padding: 0 12px; border: 1px solid #cdd2c3; border-radius: 8px; background: #fffdf4; color: var(--forest); font-size: 13px; font-weight: 700; }
.back-button:hover, .secondary-button:hover { background: #e7ede1; }
.back-button span { margin-right: 3px; }
button:disabled { opacity: .5; cursor: default; }
.round-bar { display: grid; grid-template-columns: 67px minmax(0, 1fr) 52px; align-items: center; gap: 12px; margin-bottom: 18px; font-size: 13px; font-weight: 700; }
#round-count { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.segments { display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px; }
.segment { height: 5px; border-radius: 8px; background: #e4e4d9; }
.segment.current { background: #ddbd6f; }
.segment.complete { background: #548467; }

.chord-panel {
  display: grid;
  place-items: center;
  min-height: 319px;
  padding: 23px 16px;
  border-radius: 23px;
  border: 1px solid #dedfd0;
  background: radial-gradient(ellipse at 50% 15%, #fffdf3 0%, #f0f0e5 100%);
  text-align: center;
}
.chord-content { width: 100%; }
.eyebrow { color: var(--muted); font-size: 11px; font-weight: 750; text-transform: uppercase; letter-spacing: .17em; }
#chord-name {
  margin: 5px 0 13px;
  font: 400 clamp(51px, 9vw, 86px)/.99 'Instrument Serif', Georgia, serif;
  letter-spacing: -.075em;
  color: var(--forest);
}
#chord-name:focus { outline: none; }
.note-list { display: flex; flex-wrap: wrap; justify-content: center; gap: 7px; }
.note-chip { min-width: 40px; padding: 5px 9px 6px; border-radius: 6px; background: #f5e4b5; color: #4f3a18; font-weight: 700; font-size: 14px; }
.note-chip.matched { background: #d4e8d7; color: var(--forest); }
.repetitions { margin-top: 8px; color: var(--forest); font-size: 13px; font-weight: 700; }
.panel-help { color: var(--muted); margin-top: 13px; font-size: 14px; }
.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  border: 0;
  border-radius: 10px;
  min-height: 44px;
  padding: 0 17px;
  margin-top: 17px;
  background: var(--forest);
  color: #fffdf4;
  font-weight: 700;
  font-size: 14px;
}
.primary-button:hover { background: #32624e; }
.primary-button span { font-size: 18px; }
.chord-panel.active .chord-content { padding-bottom: 27px; position: relative; }
.success { position: absolute; inset: auto 0 -8px; color: #246543; font-size: 14px; font-weight: 750; }
.success span { display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 4px; border-radius: 50%; background: #cde7d3; font-size: 15px; }
.chord-panel.solved, .chord-panel.finished { border-color: #c4d9c5; background: radial-gradient(ellipse at 50% 15%, #fbfdf1 0%, #e9f1e5 100%); }
.chord-panel.finished #chord-name { font-size: clamp(46px, 8vw, 70px); }

.rhythm-panel { padding: 15px; border: 1px solid var(--line); border-radius: 14px; background: #f2f2e8; }
.rhythm-header, .rhythm-controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; }
#rhythm-heading { color: var(--forest); font-size: 16px; }
#rhythm-tempo { font-size: 13px; }
.rhythm-score { margin-block: 8px; border: 1px solid var(--line); border-radius: 8px; background: #fffef7; }
#rhythm-notation { display: block; width: 100%; height: 91px; }
.rhythm-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.rhythm-actions .primary-button { min-height: 38px; margin: 0; padding-inline: 12px; border-radius: 8px; font-size: 13px; }
#rhythm-status { color: var(--forest); font-size: 13px; font-weight: 650; }

.keyboard-section { margin-top: 29px; }
.keyboard-header, .keyboard-footer { display: flex; justify-content: space-between; gap: 8px 14px; flex-wrap: wrap; align-items: baseline; }
.keyboard-header { margin-bottom: 11px; }
.keyboard-header h2 { font-size: 17px; letter-spacing: -.03em; }
.target-switch { display: none; }
.target-switch input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.switch-track { position: relative; width: 30px; height: 17px; border-radius: 10px; background: #69746c; }
.switch-track::after { content: ''; position: absolute; inset: 3px auto auto 3px; width: 11px; height: 11px; border-radius: 50%; background: #fffdf5; }
.target-switch input:checked + .switch-track { background: var(--forest); }
.target-switch input:checked + .switch-track::after { transform: translateX(13px); }
.target-switch input:focus-visible + .switch-track { outline: 2px solid #98701b; outline-offset: 3px; }
.legend { display: flex; align-items: center; gap: 13px; list-style: none; padding: 0; font-size: 12px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 5px; }
.swatch { width: 10px; height: 10px; border: 1px solid #202a2530; border-radius: 3px; }
.target-swatch { background: var(--target); }
.played-swatch { background: var(--played); }
.extra-swatch { background: var(--extra); }
.piano {
  --white-count: 14;
  position: relative;
  display: flex;
  height: clamp(126px, 19vw, 168px);
  overflow: hidden;
  border: solid #24352d;
  border-width: 8px 7px 7px;
  border-radius: 12px;
  background: #24352d;
  box-shadow: 0 6px 15px #24352d1a;
  touch-action: manipulation;
}
.key {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-width: 0;
  padding: 0 0 12px;
  background: #fffdf5;
  border: solid #b5b7a8;
  border-width: 0 1px 0 0;
  box-shadow: inset 0 -4px #e8e5d8;
  border-radius: 0 0 4px 4px;
  color: #515b52;
  font: 650 11px/1 'IBM Plex Mono', Consolas, monospace;
  touch-action: manipulation;
}
.key:focus-visible { outline-offset: -4px; }
.key.white { flex: 1 1 0; }
.key.white:last-child { border-right: 0; }
.key.black {
  position: absolute;
  z-index: 1;
  left: calc(var(--position) * 100% / var(--white-count));
  width: calc(62% / var(--white-count));
  height: 61%;
  transform: translateX(-50%);
  border-width: 0 1px 2px;
  border-color: #14231c;
  background: linear-gradient(#35443b, #222d27);
  box-shadow: inset 0 -3px #15231b, 0 3px 4px #24352d40;
}
:where(.keyboard-section:not(.hide-chord-targets)) .key.target { background: var(--target); color: #4f3a18; box-shadow: inset 0 -4px #0002; }
.key.played { background: var(--played); color: #184634; box-shadow: inset 0 -4px #0003; }
.key.extra { background: var(--extra); color: #53291e; box-shadow: inset 0 -4px #0003; }
.hide-chord-targets .target-legend { display: none; }
.keyboard-footer { margin-top: 11px; font-size: 12px; color: var(--muted); }
.played-notes { display: flex; align-items: baseline; gap: 8px; }
.played-notes strong { color: #2d4338; }
#played-notes { font-family: 'IBM Plex Mono', Consolas, monospace; overflow-wrap: anywhere; }
#midi-help a { color: var(--forest); }

@media (max-width: 600px) {
  .masthead, main { width: calc(100% - 30px); }
  .masthead { min-height: 77px; }
  .brand { gap: 8px; }
  .brand-mark { width: 38px; height: 38px; border-radius: 10px; }
  .brand-mark i:nth-child(2) { margin-top: 9px; }
  .brand h1 { font-size: 18px; }
  main { padding-top: 18px; }
  .round-bar { gap: 6px; margin-bottom: 13px; }
  .segments { gap: 4px; }
  .chord-panel { min-height: 270px; border-radius: 17px; }
  .keyboard-section { margin-top: 22px; }
  .legend { gap: 8px; }
  .piano { border-width: 6px 4px 5px; border-radius: 8px; }
  .key { font-size: 10px; padding-bottom: 8px; }
  .key.black { width: calc(69% / var(--white-count)); }
}

@media (max-width: 450px) and (orientation: portrait) {
  .mode-options { grid-template-columns: 1fr; }
}

@media (orientation: portrait) {
  .exercise:not([hidden]) ~ .keyboard-section .target-switch {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 34px;
    color: var(--forest);
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  body { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; padding-inline: max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right)); }
  .masthead, main { width: min(100%, 1120px); }
  .masthead { min-height: 42px; padding-block: max(3px, env(safe-area-inset-top)) 3px; gap: 9px; }
  .brand-mark { width: 33px; height: 33px; border-radius: 8px; }
  .brand-mark i:nth-child(2) { margin-top: 7px; }
  .back-button, .secondary-button, .primary-button { min-height: 34px; }
  .back-button { padding-inline: 8px; }
  .midi-status { padding: 4px 8px; }
  main { flex: 1; display: flex; flex-direction: column; padding-block: 6px max(5px, env(safe-area-inset-bottom)); }
  .home { padding: 10px 0 0; }
  .home h2 { margin-bottom: 12px; font-size: 33px; }
  .mode-options { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .mode-card { grid-template-columns: auto minmax(0, 1fr); gap: 8px; min-height: 52px; padding: 5px 8px; }
  .mode-icon { width: 32px; height: 32px; font-size: 19px; }
  .mode-card strong { font-size: 15px; }
  .mode-card small { font-size: 11px; }
  .mode-arrow { display: none; }
  .round-bar { margin-bottom: 4px; }
  .chord-panel { min-height: 46px; padding: 4px 8px; border-radius: 8px; }
  .chord-content, .chord-panel.active .chord-content { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 3px 12px; padding: 0; }
  .eyebrow, .chord-panel:not(.finished) .panel-help { display: none; }
  #chord-name, .chord-panel.finished #chord-name { font-size: clamp(27px, 5.3vw, 37px); margin: 0; }
  .note-chip { min-width: 28px; padding: 4px 6px; font-size: 12px; }
  .repetitions { margin: 0; font-size: 12px; }
  .panel-help { margin: 0; font-size: 12px; }
  .primary-button { margin: 0; padding-inline: 9px; font-size: 12px; }
  .success { position: static; font-size: 0; }
  .success span { margin: 0; }
  .rhythm-panel { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: center; gap: 2px 8px; padding: 3px 6px; border-radius: 8px; }
  .rhythm-header { grid-area: 1 / 1; gap: 4px; }
  .rhythm-header .secondary-button { min-height: 28px; padding-inline: 6px; font-size: 11px; }
  .rhythm-score { grid-area: 1 / 2 / 3; margin: 0; }
  #rhythm-notation { height: 57px; }
  .rhythm-controls { display: contents; }
  .rhythm-actions { grid-area: 2 / 1; gap: 3px; }
  .rhythm-actions .primary-button, .rhythm-actions .secondary-button { min-height: 30px; padding-inline: 6px; font-size: 11px; }
  #rhythm-status { grid-column: 1 / -1; font-size: 11px; }
  .keyboard-section { flex: 1; display: flex; flex-direction: column; margin-top: 5px; }
  .keyboard-header { display: none; }
  .piano { flex: 1 0 auto; min-height: 72px; height: auto; border-width: 5px 4px 4px; }
  .keyboard-footer { margin-top: 3px; font-size: 11px; }
  .played-notes { min-width: 0; }
  #played-notes { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}
