body {
  margin: 0;
  min-height: 100svh;
  background: #f7f7f7;
  font-family: 'Noto Sans JP', system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', Meiryo, sans-serif;
  /* タップ主体のアプリのため、ダブルタップでの文字選択・ズームを無効化する */
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* メモ欄だけは通常通りテキスト選択・コピーができるようにする */
#text-area {
  user-select: text;
  -webkit-user-select: text;
}

/* 画面の高さに合わせた純CSSレイアウト */
#page-content {
  /* デスクトップでの表示を大きくする（幅上限を拡大） */
  width: min(1100px, 96vw);
  height: 100svh;
  height: 100dvh; /* 新しい端末で動的アドレスバーにも追随 */
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-sizing: border-box;
  padding: 8px 0 12px;
}

/* Title */
#app-title {
  text-align: center;
  margin: 0;
  font-weight: 400; /* 太字をやめる */
  font-size: 1.25rem; /* ひとまわり小さく */
}

/* 共有URLで開いた時のバナー */
#shared-banner[hidden] { display: none; }
#shared-banner {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  font-size: 14px;
  color: #1e3a8a;
}
#shared-banner-text { flex: 1 1 auto; }
#shared-save-btn {
  appearance: none;
  border: 1px solid #2563eb;
  background: #2563eb;
  color: #fff;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}
#shared-dismiss-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: #1e3a8a;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

/* iOSでホーム画面に未追加の場合の案内(共有バナーと見分けやすいようグレー系に) */
#ios-install-banner[hidden] { display: none; }
#ios-install-banner {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 13px;
  color: #374151;
}
#ios-install-banner-text { flex: 1 1 auto; }
#ios-install-dismiss-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: #374151;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

/* レイアウト: 上下に並べ、最大幅調整 */
/* SVGは残り高をすべて使う */
#bassoon-wrapper {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0; /* flexアイテムが縮められるように */
}

/* SVGのサイズはラッパー幅に追従 */
#bassoon-wrapper svg {
  display: block;
  width: 100%;
  height: 100%; /* アスペクト比はSVG側で保持される */
  /* ダブルタップ操作をブラウザのズーム/文字選択ではなくアプリ側のタップとして扱う */
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* モバイルでは横幅・余白を目一杯使う */
@media (max-width: 768px) {
  #page-content { width: 100vw; padding: 0; }
}

/* 音名と楽譜コントロール */
/* 広い画面では1行に収まるためrow+wrapのまま(SVG表示エリアを圧迫しないため) */
#note-controls {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px; /* 行間 8px / 列間 12px */
  margin: 4px 0 0;
}
#note-controls .control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
#note-controls label { font-size: 14px; color: #333; }
#note-controls .checkbox { display: inline-flex; align-items: center; gap: 6px; }

/* 広い画面(1行レイアウト)ではスペーサーは不要なので消しておく */
.trill-toggle-spacer { display: none; }
.trill-toggle { flex: 0 0 auto; display: inline-flex; align-items: center; gap: 6px; }
/* Bootstrapの見た目を活かすため、セレクトの枠線/パディング指定は外す */
#octave-select,
#note-select,
#trill-octave-select,
#trill-note-select {
  box-sizing: border-box;
  font-size: 14px;
}

/* セレクトの最大幅を個別指定 */
#octave-select { min-width: 72px; max-width: 72px; padding-right: 2rem; flex: 0 0 auto; }
#note-select { min-width: 96px; max-width: 96px; padding-right: 2rem; flex: 0 0 auto; }
/* Trill用セレクトにも同じ制限を適用 */
#trill-octave-select { min-width: 72px; max-width: 72px; padding-right: 2rem; flex: 0 0 auto; }
#trill-note-select { min-width: 96px; max-width: 96px; padding-right: 2rem; flex: 0 0 auto; }
#note-controls .help {
  width: 100%;
  font-size: 12px;
  color: #666;
}

/* 図形の内側のみクリック可能にする */
#bassoon-wrapper path,
#bassoon-wrapper rect,
#bassoon-wrapper circle,
#bassoon-wrapper ellipse,
#bassoon-wrapper polygon,
#bassoon-wrapper polyline {
  pointer-events: fill;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Save / Share / Memo / その他(⋯)の4項目を1行に。記録・シェアを最優先し、
   使用頻度の低いLibrary/Downloadは⋯モーダルへ格納する(Issue #9)。 */
#controls {
  width: 100%;
  /* page-content の gap(8px) + 4px = 合計 12px の上余白 */
  margin: 4px auto 0;
  display: flex;
  gap: 12px;
  padding: 0 12px; /* 外側左右に最低12pxの余白 */
}

.control-btn,
#text-close-btn {
  appearance: none;
  border: 1px solid #d0d0d0;
  background: white;
  color: #222;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}

/* モーダル */
#text-modal[hidden] { display: none; }
#text-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  /* 背景を暗くしない（クリックブロック用途のみ） */
  background: transparent;
  z-index: 1000;
  /* 背景は操作対象にしない。誤って前面に出てもクリックを阻害しない */
  pointer-events: none;
}
.modal-dialog {
  position: fixed;
  left: 50%;
  top: 0;
  transform: translate(-50%, 0);
  width: min(720px, 100vw);
  max-height: 100dvh;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  overflow: hidden;
  z-index: 1001; /* バックドロップより上に */
  pointer-events: auto;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #eee;
}
.modal-actions { display: flex; gap: 8px; align-items: center; }
.modal-body {
  padding: 12px 16px 16px;
  overflow: auto;
  max-height: calc(100dvh - 60px); /* ヘッダーを除いた高さでスクロール */
}
#text-area {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  font-family: 'Noto Sans JP', system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', Meiryo, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-sizing: border-box;
}

/* モーダルのボタンも同じ見た目に */
#text-delete-btn,
#text-close-btn,
#library-export-btn,
#library-import-label,
#library-close-btn {
  appearance: none;
  border: 1px solid #d0d0d0;
  background: white;
  color: #222;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}

/* ライブラリ モーダル（テキストモーダルと共通の外枠ルールを流用） */
#library-modal[hidden] { display: none; }
#library-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
}
/* ⋯モーダル(下部シート)経由で開くため、Libraryモーダルも同じく下部シート形式にする */
#library-modal .modal-dialog {
  top: auto;
  bottom: 0;
  border-radius: 16px 16px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
}
#library-filter-input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 10px;
}
#library-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#library-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid #eee;
  border-radius: 8px;
}
.library-item-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  min-width: 0;
}
.library-item-note { font-weight: 600; font-size: 14px; }
.library-item-label {
  font-size: 12px;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.library-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.library-item-actions button {
  appearance: none;
  border: 1px solid #d0d0d0;
  background: white;
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.library-item-actions .library-delete-btn {
  color: #b91c1c;
  border-color: #fca5a5;
}
#library-empty-message {
  color: #666;
  font-size: 14px;
  padding: 8px 0;
  margin: 0;
}

/* その他モーダル(テキスト/ライブラリモーダルと共通の外枠ルールを流用) */
#more-modal[hidden] { display: none; }
#more-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
}
/* ⋯ボタンが画面下部にあるため、その他モーダルだけは下からせり上がる
   シート形式にする(Memo/Libraryモーダルは従来通り上部表示のまま)。
   IDセレクタで指定するため、他画面幅の.modal-dialog(border-radius:0等)
   よりも詳細度で優先される。 */
#more-modal .modal-dialog {
  top: auto;
  bottom: 0;
  border-radius: 16px 16px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
}
.more-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.more-menu li { margin: 0; }
.more-menu button {
  width: 100%;
  box-sizing: border-box;
  min-height: 48px;
  display: flex;
  align-items: center;
  padding: 0 14px;
  border: 1px solid #d0d0d0;
  border-radius: 8px;
  background: #fff;
  color: #222;
  font-size: 15px;
  cursor: pointer;
}
.more-menu button:disabled { opacity: .6; cursor: not-allowed; }

/* 幅が狭くOct/Noteが1行に収まらない画面だけ、Trill行を明示的に2段目にして位置を揃える */
/* (max-height基準の圧縮ブレークポイントには含めない: 横長だが低い画面でSVGエリアを余計に圧迫しないため) */
@media (max-width: 480px) {
  #note-controls { flex-direction: column; }
  .trill-toggle-spacer { display: block; width: 66px; flex: 0 0 auto; }
  .trill-toggle { width: 66px; }
}

/* モバイルや低い画面では余白・サイズを圧縮 */
@media (max-width: 480px), (max-height: 720px) {
  #page-content { gap: 6px; padding: 6px 0 10px; }
  #note-controls { gap: 6px 8px; }
  #note-controls .control-group { gap: 6px; }
  #note-controls select { padding: .25rem .75rem; font-size: 13px; }
  #octave-select, #trill-octave-select { min-width: 64px; max-width: 64px; padding-right: 1.9rem; }
  #note-select, #trill-note-select { min-width: 88px; max-width: 88px; padding-right: 1.9rem; }
  .modal-dialog { width: 100vw; border-radius: 0; }
}

/* --- Controls: Save / Share / Memo / その他(⋯) --- */
/* Shape & size(DOM順=視覚順。orderによる並び替えは行わない) */
#save-btn,
#share-btn,
#memo-btn {
  appearance: none;
  cursor: pointer;
  flex: 1 1 0;
  min-width: 0;
  box-sizing: border-box;
  border-radius: 12px;
  height: 48px;
  padding: 0 12px;
  font-size: 14px;
  line-height: 1;
  text-align: center;
}
#more-btn,
#clear-btn {
  appearance: none;
  cursor: pointer;
  flex: 0 0 auto;
  box-sizing: border-box;
  width: 52px;
  height: 48px;
  border-radius: 12px;
  line-height: 1;
}
#more-btn { font-size: 20px; }
#clear-btn { font-size: 12px; }

/* Focus visibility */
#save-btn:focus-visible,
#share-btn:focus-visible,
#memo-btn:focus-visible,
#more-btn:focus-visible,
#clear-btn:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }

/* Disabled state */
#save-btn:disabled,
#share-btn:disabled,
#memo-btn:disabled,
#more-btn:disabled,
#clear-btn:disabled { opacity: .6; cursor: not-allowed; }

/* Share / Save / Memo は同じ青の濃淡で優先度を表す(使用頻度: Share > Save > Memo) */
/* Share = 実塗り(最重要) */
#share-btn {
  background: #2563eb;
  color: #fff;
  border: 1px solid transparent;
}
#share-btn:hover { background: #1d4ed8; }
#share-btn:active { background: #1e40af; }

/* Save = トーン塗り(中間) */
#save-btn {
  background: #dbeafe;
  color: #1d4ed8;
  border: 1px solid transparent;
}
#save-btn:hover { background: #bfdbfe; }
#save-btn:active { background: #93c5fd; }

/* Memo = アウトライン(控えめ) */
#memo-btn {
  background: #fff;
  color: #2563eb;
  border: 1px solid #2563eb;
}
#memo-btn:hover { background: rgba(37, 99, 235, 0.06); }
#memo-btn:active { background: rgba(37, 99, 235, 0.12); }

/* その他(⋯) = Tertiary/Ghost(旧Downloadの見た目を引き継ぐ) */
#more-btn {
  background: #fff;
  color: #111827;
  border: 1px solid #d1d5db;
}
#more-btn:hover { background: #f9fafb; }
#more-btn:active { background: #f3f4f6; }

/* Clear = 通常時はGhost。誤タップで一気に消えると困るため、
   ライブラリ削除ボタンと同じ2段階確認方式(1回目で警告色に変わり、
   3秒以内の2回目で確定)を使う。armed中は警告色で「確定」を示す。 */
#clear-btn {
  background: #fff;
  color: #111827;
  border: 1px solid #d1d5db;
}
#clear-btn:hover { background: #f9fafb; }
#clear-btn:active { background: #f3f4f6; }
#clear-btn.is-armed {
  background: #fef2f2;
  color: #b91c1c;
  border-color: #fca5a5;
}
#clear-btn.is-armed:hover { background: #fee2e2; }

@media (max-width: 480px), (max-height: 720px) {
  #controls { gap: 8px; }
  #save-btn,
  #share-btn,
  #memo-btn,
  #more-btn,
  #clear-btn {
    font-size: 13px;
  }
}

/* alert()の代わりのトースト通知 */
#toast[hidden] { display: none; }
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 1100; /* .modal-dialog(1001)より上に表示する */
  max-width: min(92vw, 480px);
  box-sizing: border-box;
  padding: 12px 16px;
  border-radius: 10px;
  background: rgba(17, 24, 39, 0.94);
  color: #fff;
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none; /* 下のUI操作を塞がない */
  opacity: 0;
  transition: opacity 0.18s ease;
}
#toast.is-visible { opacity: 1; }
#toast.is-error { background: #b91c1c; }
@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
}
