/* ==========================================
   虛擬鍵盤模組
   ========================================== */
#vk-container {
    position: fixed; bottom: 0; left: 0; width: 100%;
    background: #f8f9fa; border-top: 2px solid #ddd;
    box-shadow: 0 -4px 15px rgba(0,0,0,0.15); z-index: 9999;
    font-family: sans-serif;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex; flex-direction: column;
    padding-bottom: max(15px, env(safe-area-inset-bottom));
}

#vk-container.vk-show { transform: translateY(0); }

/* 控制面板 */
#vk-controls {
    display: flex; flex-direction: column; padding: 10px 15px;
    background: #e9ecef; border-bottom: 1px solid #ccc; font-size: 13px;
}

.vk-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; width: 100%; }
.vk-main-row { justify-content: space-between; }

/* 收合設定面板動畫 */
#vk-settings-panel {
    overflow: hidden; max-height: 200px; margin-top: 10px; opacity: 1;
    transition: max-height 0.3s ease, margin-top 0.3s ease, opacity 0.3s ease;
}
#vk-settings-panel.vk-collapsed { max-height: 0; margin-top: 0; opacity: 0; pointer-events: none; }

.vk-settings-wrap { display: flex; flex-wrap: wrap; gap: 15px; align-items: center; width: 100%; }
.vk-control-group { display: flex; align-items: center; gap: 5px; }
.vk-control-group label { display: flex; align-items: center; gap: 4px; cursor: pointer; user-select: none; white-space: nowrap; }

.vk-btn {
    padding: 6px 12px; border: 1px solid #ccc; border-radius: 6px;
    background: #fff; cursor: pointer; font-size: 14px; font-weight: bold;
    color: #333; transition: all 0.2s; display: flex; align-items: center; gap: 4px;
}
.vk-btn:hover { background: #e2e6ea; }
.vk-btn.vk-active { background: #ff4d4f; color: white; border-color: #ff4d4f; }
.vk-btn.vk-toggle-active { background: #c2e7ff; color: #001d35; border-color: #7fcfff; }

.vk-input { padding: 3px 6px; border: 1px solid #ccc; border-radius: 4px; font-size: 13px; background: #fff; }

/* 視覺化節拍指示器 */
.vk-beat-container { display: flex; gap: 5px; }
.vk-beat-dot {
    width: 22px; height: 22px; border-radius: 50%; background: #d5d5d5;
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: bold; color: #666; transition: background 0.1s, transform 0.1s;
}
.vk-beat-dot.vk-active { background: #4caf50; color: #fff; transform: scale(1.1); }
.vk-beat-dot.vk-last.vk-active { background: #f44336; color: #fff; transform: scale(1.1); }

/* ==========================================
   鍵盤區域與排版魔法
   ========================================== */
#vk-keys-area {
    display: flex; 
    /* 電腦版：排成一列 (低音在左，中音在中，高音在右) */
    flex-direction: row; 
    flex-wrap: wrap; 
    gap: 4px; 
    padding: 15px 10px;
    background: #343a40; 
    max-height: 48vh; 
    overflow-y: auto;
    justify-content: center; 
    align-items: flex-end; /* 讓不同長度的按鍵底部對齊 */
    touch-action: pan-y; 
}

/* 每一層音域容器 */
.vk-tier {
    display: flex; 
    flex-wrap: nowrap; 
    justify-content: center;
}

/* 琴鍵基本設定 */
.vk-key {
    position: relative; display: flex; flex-direction: column;
    justify-content: space-between; align-items: center;
    padding-top: 5px; padding-bottom: 5px; border-radius: 0 0 4px 4px;
    cursor: pointer; user-select: none; transition: background 0.1s;
    box-shadow: inset 0 0 2px rgba(0,0,0,0.2);
}
.vk-key:active, .vk-key.vk-pressed { transform: scale(0.98); }

/* 白鍵 (電腦版) */
.vk-key.vk-white { 
    background: #fff; border: 1px solid #ccc; color: #333; 
    flex: 1 1 auto; min-width: 40px; max-width: 50px; height: 100px;
}
.vk-key.vk-white:active, .vk-key.vk-white.vk-pressed { background: #e9ecef; }

/* 黑鍵 (絕對寬度，用負 margin 拉近白鍵) */
.vk-key.vk-black {
    background: #212529; color: #fff; border: 1px solid #000;
    flex: 0 0 auto; width: 32px; height: 65px; 
    margin: 0 -16px; z-index: 2;
}
.vk-key.vk-black:active, .vk-key.vk-black.vk-pressed { background: #495057; }

/* 隱藏設定 */
.vk-no-black .vk-black { display: none !important; }
.vk-hide-labels .vk-label { display: none; }

/* 音符標籤 */
.vk-label { font-size: 14px; font-weight: bold; color: rgba(0, 0, 0, 0.4); pointer-events: none; margin-bottom: 2px;}
.vk-key.vk-black .vk-label { color: rgba(255, 255, 255, 0.5); font-size: 12px; margin-bottom: 0;}

/* 實體鍵盤提示字母 */
.vk-hint { font-size: 11px; font-weight: bold; color: #1a73e8; font-family: monospace; pointer-events: none;}
.vk-key.vk-black .vk-hint { color: #8ab4f8; font-size: 9px;}

/* ==========================================
   手機版響應式設計
   ========================================== */
@media (max-width: 768px) {
    #vk-settings-panel { max-height: 300px; }
    #vk-controls { font-size: 12px; padding: 8px 10px; }
    .vk-btn { font-size: 13px; padding: 6px 8px; }
    .vk-beat-dot { width: 18px; height: 18px; font-size: 10px; }
    
    /* 手機版：切換為三層直排，利用 column-reverse 讓高音(.1)在最上面 */
    #vk-keys-area {
        flex-direction: column-reverse; 
        align-items: center;
        gap: 10px;
        padding: 10px 5px;
    }
    .vk-tier {
        width: 100%; /* 撐開以適應螢幕寬度 */
        max-width: 500px; 
    }
    
    /* 縮小高度，適度放寬按鍵 (透過 flex 撐滿容器) */
    .vk-key.vk-white {
        height: 75px; 
        min-width: 0; /* 解除限制，讓 flex 均分寬度 */
    }
    .vk-key.vk-black {
        width: 30px; 
        height: 48px;
        margin: 0 -15px; /* 黑鍵寬度一半的負值，維持比例不破版 */
    }
    
    /* 隱藏黑鍵時的特殊優化 */
    .vk-no-black .vk-tier {
        gap: 4px; /* 加入白鍵之間的縫隙，避免黏在一起 */
    }
    .vk-no-black .vk-key.vk-white {
        height: 52px; /* 站更少版面 */
        border-radius: 6px;
    }
}