:root {
  color-scheme: dark;
  --bg: #111418;
  --fg: #e6e6e6;
  --muted: #8a8f98;
  --accent: #7fbfff;
  --button-bg: #2a2f36;
  --button-bg-hover: #353b44;
}

/* ===== ポートレート時の横向き強制 =====
   モバイルの縦持ちを検出したら、body を 90° 回転して常に横向きで表示する。
   ユーザーが端末を物理的に横向きにすれば media query から外れ、通常表示になる。 */
@media (orientation: portrait) and (max-width: 900px) {
  html, body {
    overflow: hidden;
  }
  body {
    position: absolute;
    top: 0;
    left: 100vw;
    width: 100vh;
    height: 100vw;
    transform-origin: top left;
    transform: rotate(90deg);
  }
  /* スライドインパネルは body の transform に乗るので fixed → absolute 相当の挙動になる。
     パネルの位置指定は body 内の座標系で問題なし。 */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  height: 100%;
  /* タップ時の半透明ハイライト・ダブルタップズーム・長押しメニューを抑止。
     ピンチでの拡大は許容（アクセシビリティのため鍵盤外は残す）。 */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

/* 文字選択を許可するのは発音表示と設定パネルの値表示のみ。
   ボタン類・鍵盤・タイトル等はタッチで誤選択されないよう抑止する。 */
button, h1, h2, .keyboard-wrap, .settings-link, .settings-close, .key {
  user-select: none;
  -webkit-user-select: none;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 48px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 32px;
}

h1 {
  margin: 0;
  font-weight: 400;
  letter-spacing: 0.1em;
  font-size: 1.5rem;
  color: var(--muted);
}

.page-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.settings-link {
  background: transparent;
  color: var(--muted);
  font-size: 0.9rem;
  border: 1px solid #3a3f48;
  padding: 4px 12px;
  border-radius: 3px;
  font-family: inherit;
  cursor: pointer;
}

.settings-link:hover {
  color: var(--fg);
  background: var(--button-bg);
}

.display {
  font-size: 2rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  min-height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.display.idle {
  color: var(--muted);
}

#stop {
  background: var(--button-bg);
  color: var(--fg);
  border: none;
  border-radius: 4px;
  padding: 10px 24px;
  font-size: 1rem;
  cursor: pointer;
  font-family: inherit;
}

#stop:hover { background: var(--button-bg-hover); }
#stop:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== 設定パネル（スライドイン） ===== */

.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 360px;
  max-width: 90vw;
  background: #1a1d22;
  border-left: 1px solid #2a2f36;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.4);
  padding: 24px;
  transform: translateX(100%);
  transition: transform 0.25s ease-out;
  display: flex;
  flex-direction: column;
  gap: 20px;
  z-index: 10;
}

.settings-panel.open {
  transform: translateX(0);
}

.settings-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #2a2f36;
  padding-bottom: 12px;
}

.settings-panel-header h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 400;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.settings-close {
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  font-family: inherit;
}

.settings-close:hover { color: var(--fg); }

.settings-panel-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.setting-row {
  display: grid;
  grid-template-columns: 80px 1fr 70px;
  align-items: center;
  gap: 8px 12px;
  font-size: 0.9rem;
  color: var(--fg);
}

.setting-row .setting-unit {
  grid-column: 2 / 4;
  text-align: right;
  font-size: 0.8rem;
}

.setting-label {
  color: var(--muted);
}

.setting-range {
  width: 100%;
}

.setting-number {
  width: 80px;
  background: var(--button-bg);
  color: var(--fg);
  border: 1px solid #444;
  border-radius: 3px;
  padding: 4px 8px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.setting-number:focus { outline: 1px solid var(--accent); }

.setting-unit {
  color: var(--muted);
  white-space: nowrap;
}

/* ===== 鍵盤ビジュアル ===== */

.keyboard-wrap {
  width: 100%;
  max-width: 720px;
}

.piano {
  --white-h: 160px;
  --black-h: 100px;
  --black-w: calc((100% / 12) * 0.6);
  --black-half: calc(var(--black-w) / 2);
  position: relative;
  width: 100%;
  height: var(--white-h);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;   /* スクロール／ピンチに横取りされず、すべて pointermove に流す */
}

.piano-whites {
  display: flex;
  width: 100%;
  height: 100%;
}

.piano-blacks {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: var(--black-h);
  pointer-events: none;   /* 黒鍵の隙間（透明部分）は下の白鍵に通す */
}

.key {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding-bottom: 8px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  transition: background-color 0.05s linear;
}

.key.white {
  flex: 1 1 0;
  background: #f4f1ea;
  color: #333;
  border: 1px solid #aaa;
  border-radius: 0 0 4px 4px;
  margin-right: -1px;   /* 隣接する白鍵の枠線を重ねる */
}
.key.white:last-child { margin-right: 0; }

.key.black {
  position: absolute;
  top: 0;
  width: var(--black-w);
  height: var(--black-h);
  background: #1a1d22;
  color: #ddd;
  border: 1px solid #000;
  border-radius: 0 0 3px 3px;
  z-index: 2;
  pointer-events: auto;   /* 親の .piano-blacks は none だが黒鍵自身はイベントを拾う */
}

.key .key-name {
  font-size: 0.7rem;
  opacity: 0.7;
}
.key.black .key-name { opacity: 0.6; }

.key .key-label {
  font-size: 1rem;
  font-weight: 600;
  margin-top: 4px;
}

.key.white.active {
  background: var(--accent);
  color: #0a0d12;
}
.key.black.active {
  background: #4a8ed4;
  color: #fff;
}
