/*
 * styles.css — 縦向き / 横向き / デスクトップの3レイアウト
 *
 * 鍵盤の白鍵は横並びなので、横幅がそのまま鍵の大きさになる。
 * そこで画面の形に応じて、課題パネルの置き方を変える。
 *
 *   縦向き            … パネルを鍵盤の上に置く（鍵盤は2オクターブに絞る。JS 側で切替）
 *   横向き・広い画面  … パネルを左に固定して並べる（デスクトップ）
 *   横向き・狭い画面  … パネルを引き出しにして鍵盤に重ねる（スマホ・小型タブレット）
 *
 * 既定はパネルを「組み込み」で書き、引き出しにするのは狭い横向きのときだけ。
 */

:root {
  --bg: #0f1219;
  --bg-panel: #171b26;
  --bg-raised: #212636;
  --line: #2c3244;
  --text: #e8ecf6;
  --text-dim: #949cb4;
  --accent: #f6c445;
  --accent-2: #5ec9a5;
  --danger: #f77f7f;
  --key-white: #f4f6fb;
  --key-white-edge: #c8cddb;
  --key-black: #1b1f2b;

  --gap: clamp(5px, 1vh, 10px);
  --radius: 10px;

  /*
   * ネイティブ部品（select の選択リスト、スクロールバーなど）を暗い配色で描かせる。
   * これがないと、選択リストが白背景で開いて明るい文字が読めなくなる端末がある。
   */
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP",
               system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, select { font: inherit; color: inherit; }

/* ------------------------------------------------------------------ 全体 */

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: max(var(--gap), env(safe-area-inset-top))
           max(var(--gap), env(safe-area-inset-right))
           max(var(--gap), env(safe-area-inset-bottom))
           max(var(--gap), env(safe-area-inset-left));
  gap: var(--gap);
}

.stage {
  display: flex;
  flex: 1 1 auto;
  gap: var(--gap);
  min-height: 0;
  min-width: 0;
}

/* ---------------------------------------------------------------- 上部バー */

.bar {
  display: flex;
  align-items: center;
  gap: clamp(6px, 1.2vw, 14px);
  flex: 0 0 auto;
  padding: 4px 8px;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.task {
  display: flex;
  align-items: center;
  gap: clamp(6px, 1.2vw, 14px);
  min-width: 0;
}

.chord-symbol {
  margin: 0;
  font-size: clamp(1.05rem, 4.4vh, 1.9rem);
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -0.01em;
  color: var(--accent);
  white-space: nowrap;
}

/*
 * いま出題している積み方（基本形・第1転回形・ルートレス…）。
 * 引き出しを閉じていても、どの形を練習中なのかが分かるように上部バーに出す。
 */
.form-tag {
  flex: 0 1 auto;
  padding: clamp(1px, 0.5vh, 4px) clamp(4px, 0.8vw, 8px);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: clamp(0.54rem, 1.25vh, 0.7rem);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* おまかせ中は「アプリが選んだ形」だと分かるように色を変える */
.form-tag.is-auto { border-color: var(--accent-2); color: var(--accent-2); }

.status {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  text-align: center;
  font-size: clamp(0.62rem, 1.4vh, 0.82rem);
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status.is-ok { color: var(--accent-2); }

/* ヒントを消していて鍵盤が空になっているときの案内 */
.status.is-warn { color: var(--accent); font-weight: 700; }

.status.is-clear {
  color: #17181d;
  background: var(--accent);
  border-radius: 6px;
  padding: 3px 10px;
  letter-spacing: 0.14em;
  font-weight: 800;
  flex: 0 1 auto;
}

.quick { display: flex; align-items: center; gap: 4px; margin-left: auto; }

/* ⏭ はコード進行モードのときだけ出す（flex の子でも hidden を効かせる） */
.quick .btn[hidden] { display: none; }

/* ------------------------------------------------------------------ ボタン */

.btn {
  padding: clamp(4px, 1vh, 9px) clamp(7px, 1.2vw, 14px);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: clamp(0.72rem, 1.7vh, 0.88rem);
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.btn:active { transform: translateY(1px); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #17181d; }
.btn--toggle.is-active { background: var(--accent-2); border-color: var(--accent-2); color: #10241d; }

/* ヒント非表示中。押し忘れに気づけるよう、切ってあることをはっきり見せる */
.btn--toggle.is-off { border-color: var(--accent); color: var(--accent); }
.btn--toggle.is-muted { background: var(--danger); border-color: var(--danger); color: #2a1414; }

.btn--icon {
  padding: clamp(4px, 1vh, 8px) clamp(7px, 1.1vw, 11px);
  font-size: clamp(0.85rem, 2vh, 1.05rem);
}

/* コード進行の4コード */
.prog-strip { display: flex; gap: 3px; min-width: 0; }

.prog-chip {
  padding: clamp(2px, 0.7vh, 6px) clamp(4px, 0.8vw, 9px);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: clamp(0.56rem, 1.3vh, 0.74rem);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.prog-chip.is-done { color: var(--accent-2); border-color: var(--accent-2); }
.prog-chip.is-current { background: var(--accent); border-color: var(--accent); color: #17181d; }

/* ------------------------------------------------------------------ 鍵盤 */

.board {
  flex: 1 1 auto;
  display: flex;
  min-width: 0;
  min-height: 0;
}

.keyboard-host { flex: 1 1 auto; min-height: 0; min-width: 0; }

.keyboard {
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
}

.key { cursor: pointer; }
.key .key-face { transition: fill 90ms linear; }

.key-white .key-face { fill: var(--key-white); stroke: var(--key-white-edge); stroke-width: 2; }
.key-black .key-face { fill: var(--key-black); stroke: #05070c; stroke-width: 2; }

/* 課題の音（ヒント表示中） */
.key-white.is-target .key-face { fill: #ffe9a8; }
.key-black.is-target .key-face { fill: #6b5a1f; }

/* 押している音 */
.key-white.is-held .key-face { fill: #b9c4dd; }
.key-black.is-held .key-face { fill: #3d465e; }

/* 正解の音 / 間違えた音 */
.key-white.is-correct .key-face { fill: var(--accent-2); }
.key-black.is-correct .key-face { fill: #2c7a63; }
.key-white.is-wrong .key-face { fill: #f0a2a2; }
.key-black.is-wrong .key-face { fill: #7b3838; }

.key.is-cleared .key-face { fill: var(--accent); }
.key-black.is-cleared .key-face { fill: #b8901f; }

.key-label {
  text-anchor: middle;
  font-size: 30px;
  font-weight: 600;
  fill: #7d8397;
  pointer-events: none;
}

.key-finger {
  text-anchor: middle;
  font-size: 54px;
  font-weight: 800;
  fill: #1d6b57;
  pointer-events: none;
}

.key-black .key-finger { fill: #ffe9a8; font-size: 44px; }
.key-white.is-correct .key-finger, .key-white.is-cleared .key-finger { fill: #10241d; }

/* ------------------------------------------- 課題パネル（既定は組み込み） */

.drawer {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

/* 組み込み表示のときは開閉ボタンも背景も要らない */
.drawer-backdrop,
#panelToggle,
#drawerClose { display: none; }

.drawer__head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex: 0 0 auto;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}

.brand { font-size: 0.82rem; font-weight: 700; }

/* 作者表示。見出しより小さく、色を落として右端へ寄せる */
.byline {
  margin-left: auto;
  color: var(--text-dim);
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* ✕ は文字の並びから外れるので、縦位置だけ揃え直す */
.drawer__head #drawerClose { align-self: center; }

.drawer__body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/*
 * パネルの中身は縦の flex アイテムなので、既定のままだと入りきらないときに
 * 全部が押し潰される（select が 38px → 17px になって文字が欠けていた）。
 * 縮ませずに、はみ出したぶんは .drawer__body のスクロールに任せる。
 */
.drawer__body > * { flex-shrink: 0; }

.panel__title {
  margin: 10px 0 0;
  color: var(--text-dim);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.06em;
}

.panel__title:first-child { margin-top: 0; }
.panel__title small { font-weight: 400; opacity: 0.75; margin-left: 4px; }

.panel__eyebrow { margin: 0; color: var(--text-dim); font-size: 0.74rem; line-height: 1.6; }
.prog-meta { margin: 4px 0 0; color: var(--text-dim); font-size: 0.7rem; line-height: 1.6; }

.note {
  margin: 4px 0 0;
  padding: 7px 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text-dim);
  font-size: 0.68rem;
  line-height: 1.6;
}

/* 音が出ないときの診断表示 */
.diag { margin-top: 4px; }
.diag summary { color: var(--text-dim); font-size: 0.68rem; cursor: pointer; }

.diag pre {
  margin: 6px 0 0;
  padding: 8px 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text-dim);
  font-size: 0.64rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.seg {
  display: flex;
  padding: 2px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.seg__btn {
  flex: 1 1 0;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.seg__btn.is-active { background: var(--accent); color: #17181d; }

/*
 * select は端末ごとの既定スタイルの差が大きい。
 * padding だけで高さを決めると文字の縦位置がずれるので、
 * 見た目を自前で描いて（appearance: none）高さと文字色を明示する。
 */
.field__control {
  width: 100%;
  height: 38px;
  margin: 0;
  padding: 0 32px 0 10px;
  -webkit-appearance: none;
  appearance: none;
  background-color: var(--bg-raised);
  /* 右端の ▼。文字と重ならないよう padding-right で場所を空けてある */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%23949cb4' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
  background-size: 12px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  font-size: 0.82rem;
  line-height: normal;
  text-overflow: ellipsis;
}

/* 選択リストは色を継承しないので、背景と文字色を明示しておく */
.field__control option {
  background-color: #1b2030;
  color: #e8ecf6;
}

.note-list, .finger-list { display: flex; flex-wrap: wrap; gap: 4px; }

.note-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  padding: 4px 8px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.note-chip b { font-size: 0.85rem; }
.note-chip i { font-style: normal; font-size: 0.62rem; color: var(--text-dim); }
.note-chip.is-black b { color: var(--accent); }

/* 度数（R, 3, 7, 9 …）。転回形では最低音が何の音かが要点になる */
.note-chip .deg {
  padding: 1px 4px;
  border-radius: 4px;
  background: rgba(94, 201, 165, 0.16);
  color: var(--accent-2);
  font-size: 0.58rem;
  font-weight: 700;
  line-height: 1.5;
}

/* 積み方の説明。JS 側が改行で組んでくるので pre-line で受ける */
.form-note {
  margin: 4px 0 0;
  color: var(--text-dim);
  font-size: 0.7rem;
  line-height: 1.6;
  white-space: pre-line;
}

.finger-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 0.78rem;
  color: var(--text-dim);
}

.finger-chip em {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent-2);
  color: #10241d;
  font-style: normal;
  font-weight: 800;
  font-size: 0.72rem;
}

.drawer__actions { display: flex; gap: 6px; margin-top: 12px; }
.drawer__actions .btn { flex: 1 1 auto; padding: 9px 12px; }

.build {
  margin: 10px 0 0;
  color: var(--text-dim);
  font-size: 0.64rem;
}

.build b { color: var(--text); font-weight: 700; }

.score { display: flex; gap: 16px; margin: 12px 0 0; }
.score div { display: flex; align-items: baseline; gap: 5px; }
.score dt { color: var(--text-dim); font-size: 0.68rem; }
.score dd { margin: 0; font-size: 1.05rem; font-weight: 800; }

@media (prefers-reduced-motion: reduce) {
  .key .key-face { transition: none; }
  .drawer { transition: none; }
  .btn:active { transform: none; }
}

/* ============================================================ レイアウト1
 * 縦向き: 上にパネル、下に鍵盤。
 * 鍵盤は JS 側で2オクターブ（15白鍵）に絞るので、幅が狭くても鍵が潰れない。
 */
@media (max-aspect-ratio: 1/1) {
  .stage { flex-direction: column; }

  /*
   * 鍵盤の高さは幅で決まる（白鍵15枚 = viewBox 1512、高さの上限は 872）。
   * そのぶんを aspect-ratio で先に確保し、余った高さはすべてパネルに回す。
   * こうしないと、パネルと鍵盤の間に大きな空白が残る。
   */
  .board {
    flex: 0 0 auto;
    aspect-ratio: 1512 / 872;
    max-height: 62%;
  }

  .drawer {
    order: -1;                       /* DOM 上は鍵盤の後ろにあるので前へ出す */
    flex: 1 1 auto;
  }

  /* 上部バーは幅が足りないので2段に折り返す */
  .bar { flex-wrap: wrap; row-gap: 4px; }
  .task { order: 1; }
  .quick { order: 2; }

  .status {
    order: 3;
    flex: 1 1 100%;
    text-align: left;
    white-space: normal;
  }

  .status.is-clear { flex: 1 1 100%; text-align: center; }
}

/* ============================================================ レイアウト2
 * 横向きで幅が足りない（スマホ・小型タブレット）: パネルを引き出しにする。
 */
@media (min-aspect-ratio: 1/1) and (max-width: 1099px) {
  #panelToggle, #drawerClose { display: inline-flex; }

  .drawer {
    position: fixed;
    z-index: 41;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(360px, 86vw);
    border-radius: 0;
    border-width: 0 1px 0 0;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
    transform: translateX(-101%);
    transition: transform 220ms ease;
    padding-left: env(safe-area-inset-left);
  }

  :root.drawer-open .drawer { transform: translateX(0); }

  :root.drawer-open .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(6, 8, 13, 0.6);
  }
}

/* ============================================================ レイアウト3
 * 横向きで幅に余裕がある（デスクトップ・大型タブレット）:
 * パネルを左に固定して並べる。鍵盤は残り幅いっぱい。
 */
@media (min-aspect-ratio: 1/1) and (min-width: 1100px) {
  .drawer {
    order: -1;
    flex: 0 0 clamp(250px, 21vw, 330px);
  }

  /* 画面が広いほど余白と文字に余裕を持たせる */
  .app { padding: clamp(8px, 1.2vh, 16px); gap: clamp(8px, 1.2vh, 16px); }
  .bar { padding: 6px 12px; }
  .chord-symbol { font-size: clamp(1.4rem, 3.4vh, 2.2rem); }
  .keyboard { max-height: 100%; }
}
