/* ============================================================
   Perfect Pitch の見た目
   ------------------------------------------------------------
   画面は縦に4段。上から
     ヘッダー / 出題の表示 / ボタン / 鍵盤
   鍵盤だけ高さを固定し、残りを出題の表示にあげている。
   スマホを横向きにすると画面が低くなるので、高さは vh で決めている。
   ============================================================ */

:root {
    --gold: #D4AF37;
    --bg: #0a0a0a;
    --ink: #e8e4dc;
    --good: #6fd08c;
    --bad: #e8697d;
    --panel: rgba(18, 17, 16, 0.96);
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: 'Noto Sans JP', sans-serif;
    /* 鍵盤を押したときに画面が動いたり、文字が選択されたりしないように */
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}

#app {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* スマホのアドレスバーぶんを除いた高さ。対応していなければ上の行が使われる */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background:
        radial-gradient(ellipse at 70% 0%, rgba(88, 76, 21, 0.22), transparent 55%),
        radial-gradient(ellipse at 20% 100%, rgba(44, 62, 80, 0.22), transparent 55%);
}

.hidden { display: none !important; }

/* ---------- 上のバー ---------- */
#topbar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-bottom: 1px solid rgba(212, 175, 55, 0.18);
}

#topbar-title {
    flex: 1;
    text-align: center;
    font-size: 14px;
    letter-spacing: 1px;
    color: #c8c2b6;
}

button.mini {
    min-width: 52px;
    padding: 6px 10px;
    font-size: 13px;
    color: var(--gold);
    background: transparent;
    border: 1px solid rgba(212, 175, 55, 0.45);
    border-radius: 999px;
    font-family: inherit;
    cursor: pointer;
}
button.mini:active { background: rgba(212, 175, 55, 0.15); }

/* ---------- まん中（出題） ---------- */
#stage {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 10px 14px;
    min-height: 0; /* 中身が多いときに鍵盤を押し出さないための指定 */
    overflow: hidden;
}

#progress-line { width: 100%; max-width: 620px; }

#progress-text {
    font-size: 12px;
    color: #9a938a;
    text-align: center;
    margin-bottom: 5px;
}

#progress-bar {
    height: 3px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 3px;
    overflow: hidden;
}

#progress-fill {
    height: 100%;
    width: 0;
    background: var(--gold);
    transition: width 0.35s ease;
}

/* 出題の「音の玉」。1つの塊＝縦に積んだ丸（＝和音）。 */
#phrase {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: center;
    gap: 6px;
    max-width: 100%;
}

.step {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: 3px;
    padding: 5px 4px;
    border-radius: 8px;
    border: 1px solid transparent;
    transition: background 0.15s, border-color 0.15s;
}

.step.current { border-color: rgba(212, 175, 55, 0.55); background: rgba(212, 175, 55, 0.08); }
.step.done { opacity: 0.35; }
.step.ringing { background: rgba(212, 175, 55, 0.22); border-color: rgba(212, 175, 55, 0.5); }

.dot {
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.16);
    border: 1px solid rgba(255, 255, 255, 0.25);
}
.dot.lit {
    background: var(--gold);
    border-color: var(--gold);
    box-shadow: 0 0 8px rgba(212, 175, 55, 0.7);
}

#message {
    min-height: 26px;
    font-size: 16px;
    text-align: center;
    color: #b9b2a6;
}
#message.bad { color: var(--bad); font-weight: 700; }
#message.good { color: var(--good); font-weight: 700; }
#message.listen { color: var(--gold); }

/* ---------- 操作ボタン ---------- */
#controls, #song-controls {
    flex: 0 0 auto;
    display: flex;
    gap: 10px;
    justify-content: center;
    padding: 6px 12px 10px;
}

#controls button, #song-controls button {
    padding: 10px 18px;
    font-size: 15px;
    font-family: inherit;
    color: #14120e;
    background: var(--gold);
    border: none;
    border-radius: 999px;
    cursor: pointer;
}
#controls button.ghost, #song-controls button.ghost {
    color: #cfc8ba;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.22);
}
#controls button:active, #song-controls button:active { transform: translateY(1px); }

/* ============================================================
   鍵盤 ―― 本物のピアノに寄せた作り
   ------------------------------------------------------------
   1段の中身は3つ。
     .kb-row   … 木の縁（鍵の奥にある板）
     .kb-felt  … 鍵の付け根の赤いフェルト
     .kb-keys  … 鍵の置き場（白鍵をすき間なく並べ、黒鍵をその上に乗せる）
   鍵だけを並べると「白と黒の帯」にしか見えないので、木とフェルトを足している。

   立体感の出し方：
     ・白鍵は上から下へ、ごく淡い色の変化＋下端に濃い影（手前の小口に見える）
     ・鍵と鍵の境目は線ではなく「右側の内側の影」（本物は溝が影になっている）
     ・黒鍵は上面の光沢と、白鍵に落ちる影
     ・押したときは、白鍵は奥を軸に少し傾き（rotateX）、黒鍵は沈む（translateY）
   ★touch-action: none が要る。これが無いと、押した指で画面が
     スクロールしたり拡大したりして、和音が押せなくなる。
   ============================================================ */
#keyboard-wrap {
    flex: 0 0 auto;
    padding: 0 6px 8px;
    height: clamp(120px, 32vh, 260px);
}

#keyboard {
    display: flex;
    flex-direction: column;
    gap: 6px;
    height: 100%;
    touch-action: none;
}

#keyboard.locked { opacity: 0.55; pointer-events: none; }

/* 1段ぶんの鍵盤。狭い画面では2オクターブを2段に分けるので複数並ぶ。
   周りの濃い部分が、鍵の奥にある木の板。 */
.kb-row {
    position: relative;
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-height: 0;
    padding: 6px 5px 0;
    border-radius: 8px 8px 6px 6px;
    background: linear-gradient(180deg, #3a2c22 0%, #241a13 55%, #120c08 100%);
    box-shadow:
        0 6px 20px rgba(0, 0, 0, 0.6),
        inset 0 1px 0 rgba(255, 255, 255, 0.10),
        inset 0 -1px 0 rgba(0, 0, 0, 0.6);
}

/* 鍵の付け根の赤いフェルト（本物のピアノにある帯） */
.kb-felt {
    flex: 0 0 auto;
    height: 5px;
    border-radius: 2px 2px 0 0;
    background: linear-gradient(180deg, #c04455 0%, #92283a 60%, #6d1b28 100%);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.55);
}

/* 鍵の置き場。押したときの傾きを出すために遠近感を持たせる。 */
.kb-keys {
    position: relative;
    flex: 1 1 0;
    min-height: 0;
    perspective: 900px;
}

.whites { display: flex; height: 100%; }

.key {
    position: relative;
    cursor: pointer;
    touch-action: none;
    transition: transform 0.05s ease-out, box-shadow 0.05s ease-out, background 0.08s;
}

.key.white {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 7px;
    flex: 1 1 0;
    min-width: 0;
    transform-origin: top center;
    border-radius: 0 0 6px 6px;
    /* 上はわずかに沈み、真ん中が明るく、下端で色が落ちる＝象牙の質感 */
    background: linear-gradient(180deg,
        #efeade 0%, #fdfbf4 5%, #fffefa 45%, #faf7ee 86%, #e6e0d0 96%, #cfc7b4 100%);
    box-shadow:
        inset -4px 0 5px -4px rgba(50, 40, 25, 0.55), /* 右の溝の影 */
        inset 2px 0 0 rgba(255, 255, 255, 0.85),      /* 左のふち */
        inset 0 -12px 10px -11px rgba(80, 65, 45, 0.55),
        0 2px 3px rgba(0, 0, 0, 0.3);
    color: #8a8270;
}
.key.white:last-child { box-shadow:
        inset 2px 0 0 rgba(255, 255, 255, 0.85),
        inset 0 -12px 10px -11px rgba(80, 65, 45, 0.55),
        0 2px 3px rgba(0, 0, 0, 0.3); }

/* 手前の小口（本物は鍵の先が少し立ち上がって見える） */
.key.white::after {
    content: '';
    position: absolute;
    left: 1px;
    right: 2px;
    bottom: 0;
    height: 5px;
    border-radius: 0 0 6px 6px;
    background: linear-gradient(180deg, rgba(120, 100, 70, 0.18), rgba(90, 75, 50, 0.42));
    pointer-events: none;
}

/* ★黒鍵は「押せる範囲」。この要素自体は透明で、黒く見えるのは中の .cap。
   見た目より押せる範囲を広げるための入れ子（→ script.js の BLACK_HIT）。 */
.key.black {
    position: absolute;
    top: 0;
    background: none;
    color: #cbbf9f;
    z-index: 2;
}

.key.black .cap {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    height: 100%;
    border-radius: 0 0 5px 5px;
    background: linear-gradient(180deg,
        #554c40 0%, #332c24 10%, #1d1813 55%, #0b0907 90%, #241e18 100%);
    box-shadow:
        0 5px 8px rgba(0, 0, 0, 0.65),   /* 白鍵に落ちる影 */
        inset 0 1px 0 rgba(255, 255, 255, 0.25),
        inset -2px 0 4px rgba(0, 0, 0, 0.55),
        inset 2px 0 3px rgba(255, 255, 255, 0.06);
}

/* 黒鍵の上面の光沢 */
.key.black .cap::after {
    content: '';
    position: absolute;
    left: 14%;
    right: 14%;
    top: 0;
    height: 36%;
    border-radius: 0 0 4px 4px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0));
    pointer-events: none;
}

.key.black .key-label {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 7px;
    text-align: center;
}

/* ---------- 押したとき ---------- */
/* 白鍵は奥を軸にして手前が沈む。本物の鍵の動き方。 */
.key.white.down {
    transform: rotateX(2.4deg);
    background: linear-gradient(180deg,
        #ded8c9 0%, #f2eee3 6%, #f6f2e7 45%, #ece7d9 88%, #d5cebb 100%);
    box-shadow:
        inset -4px 0 5px -4px rgba(50, 40, 25, 0.55),
        inset 0 10px 12px -8px rgba(70, 55, 35, 0.5),
        inset 0 -12px 10px -11px rgba(80, 65, 45, 0.5);
}

/* 黒鍵はまっすぐ沈み、落とす影が小さくなる */
.key.black.down .cap {
    transform: translateX(-50%) translateY(2px);
    box-shadow:
        0 2px 4px rgba(0, 0, 0, 0.55),
        inset 0 1px 0 rgba(255, 255, 255, 0.16),
        inset -2px 0 4px rgba(0, 0, 0, 0.6);
}

/* ---------- 合っていた／まちがえた ---------- */
.key.white.good { background: linear-gradient(180deg, #d9f5e0 0%, #a9e6bd 60%, #7cd39a 100%); }
.key.white.bad { background: linear-gradient(180deg, #ffe3e8 0%, #ffc6d0 60%, #f2a0ad 100%); }
.key.black.good .cap { background: linear-gradient(180deg, #4a8b60 0%, #24603a 45%, #0f3a1e 100%); }
.key.black.bad .cap { background: linear-gradient(180deg, #9c4152 0%, #66202f 45%, #35101a 100%); }

/* ---------- 曲を聴いているときに鳴っている鍵 ---------- */
/* ★これが出るのは「曲を聴く」画面だけ。出題中に光らせたら答えが見えてしまう。 */
.key.white.ringing {
    background: linear-gradient(180deg, #fff7dd 0%, #ffeeb8 55%, #f3d98d 100%);
    box-shadow:
        inset 0 0 20px rgba(212, 175, 55, 0.55),
        0 0 16px rgba(212, 175, 55, 0.45);
}
.key.black.ringing .cap {
    background: linear-gradient(180deg, #a58c42 0%, #6b5622 50%, #3d3011 100%);
    box-shadow: 0 0 14px rgba(212, 175, 55, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

/* 3回まちがえたときに出る「ここだよ」の光 */
.key.white.hint, .key.black.hint .cap { animation: hintPulse 0.9s ease-in-out infinite; }
@keyframes hintPulse {
    0%, 100% { box-shadow: inset 0 0 0 0 rgba(212, 175, 55, 0); }
    50% { box-shadow: inset 0 0 22px 3px rgba(212, 175, 55, 0.85); }
}

/* 音の名前は、出す設定のときだけ見せる（既定は消しておく。
   見えていると「聞く」のではなく「読む」ゲームになってしまうため） */
.key-label { font-size: 11px; opacity: 0; pointer-events: none; }
#keyboard.show-names .key-label { opacity: 1; }
#keyboard.wide .key-label { font-size: 9px; }

/* ============================================================
   かぶせる画面（タイトル・結果・ごほうび）
   ============================================================ */
.overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(6, 6, 6, 0.88);
    backdrop-filter: blur(3px);
    overflow-y: auto;
}

.panel {
    width: min(560px, 100%);
    max-height: 100%;
    overflow-y: auto;
    padding: 22px 20px 26px;
    background: var(--panel);
    border: 1px solid rgba(212, 175, 55, 0.28);
    border-radius: 16px;
    text-align: center;
}

.panel h1 {
    font-family: 'Cormorant Garamond', serif;
    font-size: clamp(2.1em, 8vw, 3.2em);
    margin: 0 0 10px;
    color: #fff;
    letter-spacing: 2px;
    text-shadow: 0 0 8px rgba(212, 175, 55, 0.65), 0 0 26px rgba(212, 175, 55, 0.4);
}

.panel h2 {
    font-family: 'Cormorant Garamond', serif;
    font-size: 2em;
    margin: 0 0 12px;
    color: #fff;
    letter-spacing: 2px;
}

.lead { font-size: 14px; line-height: 1.85; color: #b9b2a6; margin: 0 0 18px; }
.lead b { color: var(--gold); }
.hint { font-size: 12px; color: #8d867c; margin: 14px 0 0; }

.chooser { margin: 0 0 16px; }
.chooser-label {
    font-size: 11px;
    letter-spacing: 2px;
    color: #8d867c;
    margin-bottom: 8px;
}

.btn-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}

.btn-row button {
    flex: 1 1 140px;
    max-width: 220px;
    padding: 12px 10px;
    font-family: inherit;
    font-size: 13px;
    line-height: 1.5;
    color: #cfc8ba;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-radius: 12px;
    cursor: pointer;
}
.btn-row button b { display: block; font-size: 17px; color: #fff; margin-bottom: 3px; }
.btn-row button span { display: block; font-size: 11px; color: #9a938a; }
.btn-row button i.best { display: block; font-style: normal; font-size: 10px; color: var(--gold); margin-top: 4px; }
.btn-row button.on {
    color: #fff;
    border-color: var(--gold);
    background: rgba(212, 175, 55, 0.14);
    box-shadow: 0 0 14px rgba(212, 175, 55, 0.25);
}

.main-actions { margin-top: 20px; }
.main-actions button { flex: 0 1 auto; padding: 12px 22px; font-size: 15px; }
.main-actions button.primary { color: #14120e; background: var(--gold); border-color: var(--gold); font-weight: 700; }

.switch { font-size: 13px; color: #b9b2a6; cursor: pointer; }
.switch input { margin-right: 6px; transform: translateY(1px); }

/* ---------- 結果 ---------- */
#result-rank {
    font-family: 'Cormorant Garamond', serif;
    font-size: 4.2em;
    line-height: 1;
    margin: 4px 0 6px;
    color: var(--gold);
    text-shadow: 0 0 18px rgba(212, 175, 55, 0.55);
}
#result-rank.rank-S { color: #ffe9a8; }
#result-rank.rank-C { color: #b6afa2; text-shadow: none; }

#result-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    margin-bottom: 16px;
}
#result-table th, #result-table td {
    padding: 8px 6px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    text-align: left;
}
#result-table th { color: #8d867c; font-weight: 400; white-space: nowrap; }
#result-table td { color: var(--ink); text-align: right; }

.chip {
    display: inline-block;
    margin: 3px;
    padding: 5px 12px;
    font-size: 13px;
    color: var(--ink);
    background: rgba(232, 105, 125, 0.14);
    border: 1px solid rgba(232, 105, 125, 0.4);
    border-radius: 999px;
}
.chip b { color: var(--bad); }
.chip.quiet { background: rgba(255, 255, 255, 0.05); border-color: rgba(255, 255, 255, 0.15); color: #8d867c; }

/* 2オクターブを2段に分けているときは、鍵盤に高さを多めに渡す
   （1段ぶんの高さが半分になるため。実測：縦持ちで1段128pxほど） */
@media (orientation: portrait) and (max-width: 700px) {
    #keyboard-wrap { height: clamp(190px, 40vh, 340px); }
}

/* ---------- 横向き・小さい画面 ---------- */
@media (max-height: 480px) {
    /* スマホ横向き。鍵盤に高さを回し、説明は詰める。 */
    #keyboard-wrap { height: clamp(110px, 42vh, 200px); }
    #topbar { padding: 4px 10px; }
    #message { min-height: 20px; font-size: 14px; }
    #controls { padding: 4px 12px 6px; }
    #controls button, #song-controls button { padding: 7px 14px; font-size: 13px; }
    .panel { padding: 14px 16px 18px; }
    .lead { margin-bottom: 10px; line-height: 1.6; }
    .panel h1 { font-size: 1.9em; }
    .chooser { margin-bottom: 10px; }
}

@media (min-width: 700px) {
    .dot { width: 13px; height: 13px; }
}
