:root {
  --bg: #14101a;
  --bg-elev: #1e1828;
  --bg-panel: #261f33;
  --text: #f4efe8;
  --muted: #b9aec8;
  --accent: #e8b86d;
  --accent-deep: #c9893a;
  --record: #e2556a;
  --record-glow: rgba(226, 85, 106, 0.45);
  --play: #5ecf9a;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --white-key: linear-gradient(180deg, #fffdf8 0%, #f2ebe0 55%, #e4d7c6 100%);
  --white-active: linear-gradient(180deg, #ffe7b8 0%, #f0c878 100%);
  --black-key: linear-gradient(180deg, #3a3344 0%, #16121c 70%, #0b090f 100%);
  --black-active: linear-gradient(180deg, #7a5a2a 0%, #3a2a14 100%);
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: "Noto Sans TC", "PingFang HK", "Hiragino Sans CNS", "Microsoft JhengHei", system-ui, sans-serif;
}

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

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(232, 184, 109, 0.14), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(120, 90, 180, 0.18), transparent 50%),
    var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
}

.app {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.25rem 1rem calc(1.5rem + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem 1.25rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-mark {
  font-size: 2rem;
  filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.35));
}

h1 {
  margin: 0;
  font-size: clamp(1.45rem, 4vw, 1.9rem);
  font-weight: 700;
  letter-spacing: 0.04em;
}

.subtitle {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.status {
  font-size: 0.88rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
}

.status.recording {
  color: #ffd0d8;
  border-color: rgba(226, 85, 106, 0.45);
  box-shadow: 0 0 0 1px var(--record-glow);
}

.status.playing {
  color: #c9ffe6;
  border-color: rgba(94, 207, 154, 0.4);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 999px;
  padding: 0.7rem 1.15rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease, opacity 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 44px;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.18);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.btn-record .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--record);
  box-shadow: 0 0 0 0 var(--record-glow);
}

.btn-record[aria-pressed="true"] {
  background: rgba(226, 85, 106, 0.18);
  border-color: rgba(226, 85, 106, 0.55);
  color: #ffd7de;
}

.btn-record[aria-pressed="true"] .dot {
  animation: pulse 1.1s ease-out infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 var(--record-glow); }
  70% { box-shadow: 0 0 0 10px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

.btn-play {
  background: linear-gradient(180deg, rgba(94, 207, 154, 0.22), rgba(94, 207, 154, 0.08));
  border-color: rgba(94, 207, 154, 0.35);
}

.btn-stop {
  background: rgba(255, 255, 255, 0.04);
}

.btn-clear {
  margin-left: auto;
}

.jianpu-panel,
.keys-help {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}

.jianpu-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.65rem;
}

.jianpu-head h2,
.keys-help h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}

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

.jianpu-display {
  min-height: 3.2rem;
  padding: 0.85rem 1rem;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.06);
  font-size: clamp(1.25rem, 3.5vw, 1.65rem);
  letter-spacing: 0.12em;
  line-height: 1.55;
  font-family: "Noto Sans TC", "PingFang HK", ui-monospace, monospace;
  word-break: break-word;
  white-space: pre-wrap;
}

.jianpu-display .active-note {
  color: var(--accent);
  text-shadow: 0 0 12px rgba(232, 184, 109, 0.45);
  font-weight: 700;
}

.convention {
  margin-top: 0.75rem;
  color: var(--muted);
  font-size: 0.88rem;
}

.convention summary {
  cursor: pointer;
  color: var(--accent);
  user-select: none;
}

.convention-body {
  margin-top: 0.55rem;
  display: grid;
  gap: 0.4rem;
}

.convention-body p {
  margin: 0;
  line-height: 1.55;
}

.piano-wrap {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  border-radius: calc(var(--radius) + 4px);
  padding: 0.35rem;
  background: linear-gradient(180deg, #2a2236, #17121f);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  touch-action: none;
}

.piano {
  position: relative;
  height: clamp(180px, 38vw, 260px);
  min-width: 320px;
  width: 100%;
  margin: 0 auto;
  user-select: none;
  touch-action: none;
}

.key {
  position: absolute;
  top: 0;
  border-radius: 0 0 10px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 0.15rem;
  padding-bottom: 0.65rem;
  cursor: pointer;
  transition: filter 0.08s ease, transform 0.06s ease;
  -webkit-user-select: none;
  user-select: none;
}

.key.white {
  height: 100%;
  background: var(--white-key);
  border: 1px solid #c9bba8;
  border-top: none;
  box-shadow:
    inset 0 -8px 14px rgba(0, 0, 0, 0.08),
    0 10px 16px rgba(0, 0, 0, 0.28);
  color: #4a3f35;
  z-index: 1;
}

.key.black {
  height: 62%;
  background: var(--black-key);
  border: 1px solid #000;
  box-shadow:
    inset 0 -6px 10px rgba(255, 255, 255, 0.06),
    0 8px 14px rgba(0, 0, 0, 0.45);
  color: #f0e6d6;
  z-index: 2;
}

.key.white.active,
.key.white.playback {
  background: var(--white-active);
  filter: brightness(1.02);
  transform: translateY(2px);
  box-shadow:
    inset 0 -4px 10px rgba(0, 0, 0, 0.12),
    0 4px 8px rgba(0, 0, 0, 0.25);
}

.key.black.active,
.key.black.playback {
  background: var(--black-active);
  transform: translateY(2px);
  filter: brightness(1.15);
}

.key .note-name {
  font-size: 0.72rem;
  font-weight: 700;
  opacity: 0.85;
}

.key .jianpu-label {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.key.black .note-name {
  font-size: 0.62rem;
}

.key.black .jianpu-label {
  font-size: 0.78rem;
}

.key .kbd-hint {
  font-size: 0.65rem;
  opacity: 0.55;
  font-weight: 600;
  margin-top: 0.1rem;
}

.keys-help .kbd-rows {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.75rem;
}

.kbd-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
}

.kbd-label {
  width: 3rem;
  color: var(--muted);
  font-size: 0.85rem;
}

kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2rem;
  height: 2rem;
  padding: 0 0.45rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
}

.kbd-row .gap {
  width: 1.1rem;
}

.hint {
  margin: 0.75rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
}

.footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  opacity: 0.85;
}

.footer p {
  margin: 0;
}

@media (max-width: 560px) {
  .btn-clear {
    margin-left: 0;
  }

  .key .kbd-hint {
    display: none;
  }

  .status {
    width: 100%;
    text-align: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn,
  .key,
  .btn-record .dot {
    transition: none;
    animation: none;
  }
}
