:root {
  --bg: #f4f1ea;
  --paper: #fffdf7;
  --line: #d6cfbf;
  --text: #2b2b2b;
  --muted: #6b6b6b;
  --blue: #1e88e5;
  --green: #2e7d32;
  --red: #d32f2f;
  --orange: #ef6c00;
  --gutter: 10px;
  font-family: "Microsoft JhengHei UI", "PingFang TC", "Noto Sans TC", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { overflow: hidden; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px max(var(--gutter), env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
}

/* ---- 設定列 ---- */
.settings { display: flex; align-items: flex-start; gap: 10px; }
.settings-body { flex: 1 1 auto; min-width: 0; }
.settings-actions { display: flex; gap: 8px; flex: 0 0 auto; padding-top: 2px; }
.settings-toggle { display: none; }
.settings .row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; padding: 2px 0; }
.settings label { font-size: 14px; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px; }
.settings select { font: inherit; font-size: 14px; padding: 3px 4px; border: 1px solid #b9b2a3; border-radius: 4px; background: #fff; }
.settings .check input { width: 16px; height: 16px; }
.settings .volume input { width: 110px; }
.link { background: none; border: none; color: var(--blue); font-size: 14px; padding: 2px 4px; cursor: pointer; text-decoration: none; }
.link:hover { text-decoration: underline; }

button {
  font: inherit; font-size: 15px; padding: 7px 14px; border-radius: 6px;
  background: #fff; border: 1px solid #b9b2a3; color: var(--text); cursor: pointer;
  touch-action: manipulation;
}
button:hover:not(:disabled) { background: #eef4ff; }
button:disabled { color: #aaa; cursor: default; }
button.primary { background: var(--blue); color: #fff; border: none; font-weight: bold; }
button.primary:hover:not(:disabled) { background: #1976d2; }
button.primary:disabled { background: #9cc4ec; }

/* ---- 樂譜 ---- */
.score-area {
  flex: 1 1 auto; min-height: 180px; display: flex; gap: 6px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 4px;
}
.score { flex: 1 1 auto; min-width: 0; height: 100%; display: flex; align-items: center; justify-content: center; }
.score svg { width: 100%; height: 100%; }
/* 主樂譜區可雙擊提交：停用雙擊放大與文字選取（兩指縮放仍可用） */
#score { touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; cursor: default; }
.score-area.submit-flash { animation: submit-flash 0.35s ease-out; }
@keyframes submit-flash { from { background: #d6e9fb; } to { background: var(--paper); } }
.placeholder { color: #888; }
.preview { flex: 0 0 240px; display: flex; flex-direction: column; border-left: 1px dashed var(--line); padding-left: 4px; }
.preview[hidden] { display: none; }
.preview-title { text-align: center; color: var(--muted); font-size: 13px; }
.preview .score { flex: 1 1 auto; min-height: 0; }

.limit-bar { height: 6px; background: #e6e0d4; border-radius: 3px; overflow: hidden; }
.limit-bar[hidden] { display: none; }
#limitFill { height: 100%; width: 100%; background: var(--blue); transform-origin: left; }

.info { display: flex; flex-wrap: wrap; gap: 4px 24px; font-size: 15px; color: #333; }
.feedback { font-size: 15px; line-height: 1.5; min-height: 1.5em; max-height: 7.5em; overflow-y: auto; padding: 0 6px; }
.rotate-hint { display: none; font-size: 13px; color: var(--orange); }

/* ---- 鍵盤 ---- */
#keyboard { flex: 0 0 auto; height: clamp(130px, 28vh, 230px); }
.keyboard-scroll { overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
.keyboard { position: relative; height: 100%; user-select: none; -webkit-user-select: none; }
.keyboard-strip { position: absolute; left: 0; right: 0; top: 0; height: 0; touch-action: pan-x; }
.key { position: absolute; top: 0; touch-action: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.key.white {
  height: 100%; background: #fff; border: 1px solid #555; border-radius: 0 0 4px 4px; z-index: 1;
  display: flex; align-items: flex-end; justify-content: center;
}
.key.black { height: 62%; background: #1b1b1b; border: 1px solid #000; border-radius: 0 0 3px 3px; z-index: 2; }
.key.white.pressed { background: #e3eefc; }
.key.black.pressed { background: #444; }
.key.selected { background: var(--blue) !important; }
.key.correct { background: var(--green) !important; }
.key.wrong { background: var(--red) !important; }
.key.missed { background: var(--orange) !important; }
/* 提示：金色外框＋音名（選取後仍看得到外框）。
   音名標籤不蓋到任何黑鍵：白鍵的標在琴鍵最下方；黑鍵的放在黑鍵正下方的白鍵區，用 ▲ 指向黑鍵。 */
.key.hint { box-shadow: inset 0 0 0 3px #f9a825; }
.key.black.hint { box-shadow: inset 0 0 0 2px #f9a825, 0 0 0 2px #f9a825; }
.hint-label {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 3px;
  background: #f9a825; color: #222; font-size: 11px; font-weight: bold; line-height: 1.2;
  padding: 1px 3px; border-radius: 3px; white-space: nowrap; pointer-events: none; z-index: 5;
  animation: hint-pulse 1s ease-in-out infinite alternate;
}
.key.black .hint-label { bottom: auto; top: calc(100% + 2px); }
.key.black .hint-label.low { top: calc(100% + 2px + 1.45em); } /* 與左邊相鄰黑鍵的標籤錯開 */
@media (max-height: 520px) { .hint-label { font-size: 10px; padding: 0 2px; } }
@keyframes hint-pulse { from { opacity: 1; } to { opacity: 0.55; } }
.key-label { font-size: 11px; color: #666; text-align: center; padding-bottom: 6px; pointer-events: none; line-height: 1.2; }
.key.selected .key-label, .key.correct .key-label, .key.wrong .key-label, .key.missed .key-label { color: #fff; }

/* ---- 按鈕列 ---- */
.buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.buttons .big { min-width: 140px; }

dialog { border: 1px solid var(--line); border-radius: 8px; padding: 16px 20px; max-width: min(440px, 92vw); }
dialog h2 { margin-top: 0; font-size: 18px; }
dialog .note { color: var(--muted); font-size: 13px; }
#statsBody { line-height: 1.7; }

/* ---- 窄螢幕（手機直向） ---- */
@media (max-width: 760px) {
  .preview { flex-basis: 150px; }
  .settings label, .settings select, .link { font-size: 13px; }
  .info, .feedback { font-size: 13px; }
}
@media (max-width: 600px) and (orientation: portrait) {
  /* 手機直向：版面改為可上下捲動，「你的選擇」放到樂譜下方 */
  body { overflow: auto; }
  .app { height: auto; min-height: 100dvh; }
  .score-area { flex: 0 0 auto; flex-direction: column; }
  .score-area > .score { height: 190px; }
  .preview { flex: 0 0 auto; border-left: none; border-top: 1px dashed var(--line); padding: 4px 0 0; }
  .preview .score { height: 130px; }
  .feedback { max-height: none; }
  #keyboard { height: 210px; }
  .buttons button { padding: 7px 10px; }
  .rotate-hint { display: block; }
  .keyboard-strip { height: 22px; background: repeating-linear-gradient(90deg, #ddd 0 6px, #eee 6px 12px); z-index: 3; }
  .keyboard .key { top: 22px; }
  .key.white { height: calc(100% - 22px); }
  .key.black { height: calc((100% - 22px) * 0.62); }
}
/* ---- 矮螢幕（手機橫向）：樂譜優先 ----
   上方設定列整個收起（⚙ 與「開始」移到最下方按鈕列，見 main.js），
   資訊列隱藏（統計在 ⚙ → 學習統計），把高度留給樂譜。 */
@media (max-height: 520px) {
  .app { gap: 4px; padding-top: 4px; }
  .settings-toggle { display: inline-block; }
  .settings:not(.open) { display: none; }
  body.settings-open { overflow: auto; }
  body.settings-open .app { height: auto; }
  .buttons { flex-wrap: nowrap; gap: 6px; }
  .buttons button { padding: 5px 10px; font-size: 14px; white-space: nowrap; }
  .buttons .big { min-width: 0; }
  .buttons .settings-actions { margin-left: auto; padding-top: 0; gap: 6px; }
  .buttons .long { display: none; }   /* ♪、⚙ 只顯示圖示 */
  .buttons #start { display: none; }  /* 與「下一題」相同功能 */
  .score-area { min-height: 100px; padding: 2px; }
  .preview { flex-basis: 140px; }
  .preview-title { font-size: 11px; }
  .info { display: none; }
  /* 回饋只佔一行，點一下展開全文（例如整題結束的總結） */
  .feedback { font-size: 12px; line-height: 1.5; min-height: 0; max-height: 1.5em; overflow: hidden;
              white-space: nowrap; text-overflow: ellipsis; cursor: pointer; padding: 0 4px; }
  .feedback br { display: none; }
  .feedback.expanded { white-space: normal; max-height: 40vh; overflow-y: auto; }
  .feedback.expanded br { display: inline; }
  .buttons button { padding: 4px 9px; }
  #keyboard { height: clamp(84px, 26vh, 150px); }
  .key-label { font-size: 9px; padding-bottom: 3px; }
}
