: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; }
.settings input[type=number] { width: 64px; font: inherit; font-size: 14px; padding: 3px 4px; border: 1px solid #b9b2a3; border-radius: 4px; }
.link { background: none; border: none; color: var(--blue); font-size: 14px; padding: 2px 4px; cursor: pointer; text-decoration: none; }
.link:hover { text-decoration: underline; }
.build-info { font-size: 12px; color: var(--muted); white-space: nowrap; }

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 {
  position: relative; 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; }
/* 暫停：蓋住樂譜，暫停中不能先偷看下一個音 */
.pause-overlay {
  position: absolute; inset: 0; z-index: 10; border: none; border-radius: 6px;
  background: var(--paper); color: var(--blue); font-size: 26px; font-weight: bold; line-height: 1.6;
}
.pause-overlay span { font-size: 14px; font-weight: normal; color: var(--muted); }
.pause-overlay:hover:not(:disabled) { background: var(--paper); }
.pause-overlay[hidden] { display: none; }
.keyboard.blocked { opacity: 0.45; }
.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.white.in-lesson { background: #e8f1fb; }
.key.black.in-lesson { background: #3a4a5c; }
.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; }

/* 收音：開啟時按鈕變紅，鍵盤上聽到的鍵閃一下 */
button.mic.on {
  background: #fdecea linear-gradient(var(--red), var(--red)) no-repeat left bottom / var(--mic-level, 0%) 4px;
  border-color: var(--red); color: var(--red); min-width: 64px;
}
.mic-note { font-weight: bold; }
body.mic-mode .key { cursor: not-allowed; }
.key.heard-wrong { background: var(--red) !important; } /* 收音模式：彈錯的音紅色閃一下 */ /* 收音模式：螢幕鍵盤不能按 */
.key.heard { box-shadow: inset 0 0 0 4px var(--red); }
body.write-mode #mic { display: none; } /* 寫譜模式是點五線譜，不收音 */

/* 寫譜模式（試作）：不用鍵盤，樂譜放大；點譜表放音符 */
.write-only { display: none; }
body.write-mode .write-only { display: inline-block; }
body.write-mode #keyboard, body.write-mode .preview { display: none; }
body.write-mode #score { cursor: crosshair; }
/* 寫譜（手機橫向）：樂譜優先，回饋文字隱藏（答錯看紅色的音；提示時才顯示），四周留白縮小 */
@media (max-height: 520px) {
  body.write-mode .feedback:not(.keep) { display: none; }
  body.write-mode .app { gap: 2px; padding-top: 2px; }
  body.write-mode .score-area { padding: 0; border-radius: 4px; }
  /* 下方按鈕列整個隱藏（答題靠點譜表與自動提交），右上角的 ☰ 可以叫出來 */
  body.write-mode:not(.show-bar) .buttons { display: none; }
  body.write-mode .bar-toggle { display: block; }
}
.bar-toggle {
  display: none; position: absolute; top: 2px; right: 2px; z-index: 9;
  padding: 2px 8px; font-size: 16px; line-height: 1.2; opacity: 0.75;
}
body.show-bar .bar-toggle { opacity: 1; background: #eef4ff; }

/* 設定列裡被隱藏的項目（標籤本身有 display 設定，需明確隱藏） */
.settings [hidden] { display: none !important; }

/* ---- 按鈕列 ---- */
.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; }
.dialog-buttons { display: flex; justify-content: space-between; gap: 8px; }
button.danger { color: var(--red); border-color: var(--red); }
button.danger:hover:not(:disabled) { background: #fdecea; }
#statsBody { line-height: 1.7; }
#courseDialog { max-width: min(560px, 94vw); }
.course-unit { margin: 10px 0 4px; font-size: 14px; color: var(--muted); }
.course-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.course-cell { padding: 6px 4px; font-size: 13px; line-height: 1.3; text-align: center; border-radius: 6px; }
.course-cell b { display: block; font-size: 15px; }
.course-cell.passed { background: #e6f4ea; border-color: var(--green); color: var(--green); }
.course-cell.current { border: 2px solid var(--blue); color: var(--blue); }
.course-cell.later { color: #aaa; cursor: default; }
.lesson-progress { margin-top: 10px; font-size: 15px; }
@media (max-height: 520px) { .course-cell { padding: 3px 2px; font-size: 11px; } .course-cell b { font-size: 13px; } }
.summary-grade { font-size: 20px; font-weight: bold; margin-bottom: 8px; }
.summary-table { border-collapse: collapse; font-size: 15px; line-height: 1.6; }
.summary-table th { text-align: left; font-weight: normal; color: var(--muted); padding: 2px 12px 2px 0; white-space: nowrap; vertical-align: top; }
.summary-table td { padding: 2px 0; }
#summaryDialog .dialog-buttons { margin-top: 14px; }
.summary-info { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line); }
.summary-info .summary-table { font-size: 13px; color: var(--muted); }
@media (max-height: 520px) {
  dialog { padding: 10px 14px; max-height: 94vh; }
  dialog h2 { margin-bottom: 6px; font-size: 16px; }
  .summary-grade { font-size: 16px; margin-bottom: 4px; }
  .summary-table { font-size: 13px; line-height: 1.45; }
  #summaryDialog .dialog-buttons { margin-top: 8px; }
}

/* ---- 窄螢幕（手機直向） ---- */
@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; }
}
