:root {
    --bg-body: #ffffff;
    --bg-sidebar: #f0f4f9;
    --bg-surface: #ffffff;
    --primary-bg: #c2e7ff;
    --primary-text: #001d35;
    --text-main: #1f1f1f;
    --text-sub: #444746;
    --border: #e0e3e1;
    --radius-lg: 24px;
    --radius-md: 12px;
    --sidebar-width: 280px;
    --header-height: 60px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: twhei-s, TWHEI, "台灣黑體", tauhu-oo, "Google Sans", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-main);
    min-height: 100vh;
    overflow-y: auto; 
}

.app-container { display: flex; min-height: 100vh; width: 100%; }

/* ==========================================
   Sidebar (側邊欄)
   ========================================== */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--bg-sidebar);
    display: flex; flex-direction: column; padding: 20px 12px;
    flex-shrink: 0; 
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), margin-left 0.3s ease;
    border-right: none; position: sticky; top: 0; height: 100vh; 
    z-index: 200; 
}
.sidebar.collapsed { margin-left: calc(var(--sidebar-width) * -1); }

.sidebar-header { margin-bottom: 24px; padding: 16px 16px 8px 16px; }

.mobile-sidebar-title {
    display: none; font-size: 1.2rem; font-weight: bold; color: var(--text-main);
    margin-bottom: 16px; padding-left: 8px;
}

.sidebar-tabs {
    display: flex; margin: 0 16px 16px 16px; background-color: #f1f3f4; 
    border-radius: 20px; padding: 4px;
}

.side-tab-btn {
    flex: 1; border: none; background: none; padding: 6px 0;
    font-size: 0.9rem; color: #5f6368; cursor: pointer;
    border-radius: 16px; font-weight: 500; transition: all 0.2s ease;
}
.side-tab-btn.active {
    background-color: #ffffff; color: #1a73e8; box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.side-list-view { display: none; flex: 1; overflow-y: auto; flex-direction: column; }
.side-list-view.active { display: flex; animation: fadeIn 0.2s ease; }

#new-song-btn {
    width: calc(100% - 32px); margin: 0 16px 12px 16px; padding: 10px;
    background-color: #e8f0fe; color: #1a73e8; border: none;
    border-radius: 8px; font-weight: 600; display: flex; align-items: center;
    justify-content: center; gap: 8px; transition: background 0.2s; cursor: pointer;
}
#new-song-btn:hover { background-color: #d2e3fc; }

.song-list-label { padding: 0 16px 8px 16px; font-size: 0.85rem; font-weight: 500; color: var(--text-sub); }
.song-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }

.song-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 16px; border-radius: 100px; cursor: pointer;
    font-size: 0.9rem; color: var(--text-main); transition: background 0.2s; margin: 0 4px;
}
.song-item:hover { background-color: #e0e5eb; }
.song-item.active { background-color: var(--primary-bg); color: var(--primary-text); font-weight: 500; }

.delete-song-btn { opacity: 0; background: none; border: none; padding: 6px; color: var(--text-sub); cursor: pointer; border-radius: 50%; }
.song-item:hover .delete-song-btn { opacity: 1; }
.delete-song-btn:hover { background-color: rgba(0,0,0,0.1); color: #b3261e; }

.library-item { color: #5f6368; background-color: transparent; }
.library-item:hover { background-color: #f0f4f9; color: #0b57d0; }

/* ==========================================
   Header (頂部欄)
   ========================================== */
.top-bar {
    display: flex; align-items: center; justify-content: space-between;
    height: var(--header-height); padding: 0 16px;
    position: sticky; top: 0; background: rgba(255,255,255,0.95);
    z-index: 20; backdrop-filter: blur(5px); border-bottom: 1px solid transparent;
}

.main-content {
    flex: 1; display: flex; flex-direction: column;
    position: relative; min-width: 0; background-color: var(--bg-body); min-height: 100vh;
}

.header-left { display: flex; align-items: center; gap: 8px; z-index: 2; position: relative; }
.app-name { 
    font-size: 1rem; font-weight: 500; color: var(--text-sub); 
    margin: 0; white-space: nowrap; display: flex; align-items: baseline; 
}

.header-center { 
    position: absolute; left: 50%; transform: translateX(-50%);
    width: 100%; max-width: 300px; z-index: 1;
    display: flex; justify-content: center; pointer-events: none;
}
.title-input-wrapper { position: relative; display: flex; align-items: center; width: 100%; pointer-events: auto; }
.doc-title-input {
    border: none; font-size: 1.1rem; color: var(--text-main); font-weight: 500;
    padding: 6px 12px; border-radius: 8px; background: transparent;
    text-align: center; width: 100%; transition: 0.2s;
    text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
}
.doc-title-input:hover { background-color: #f0f4f9; }
.doc-title-input:focus { outline: 2px solid #0b57d0; background-color: white; }

.header-right { display: flex; align-items: center; gap: 8px; z-index: 2; position: relative; }

/* ==========================================
   Workspace & Panels
   ========================================== */
.workspace { flex: 1; display: flex; gap: 16px; padding: 0 20px; align-items: stretch; }
.layout-horizontal { flex-direction: row; }
.layout-vertical { flex-direction: column; }

.panel {
    flex: 1; display: flex; flex-direction: column;
    background: var(--bg-surface); border-radius: var(--radius-lg); border: 1px solid var(--border);
    min-width: 0; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.panel.collapsed { flex: 0 0 52px; overflow: hidden; min-height: 52px; }
.panel.collapsed + .panel, .panel:has(+ .panel.collapsed) { flex: 5; }
.panel.collapsed .toggle-panel-btn svg { transform: rotate(-90deg); }
.panel.collapsed .panel-actions, .panel.collapsed .panel-content-wrapper { display: none; }
.panel-content-wrapper { display: flex; flex-direction: column; flex: 1; }

.panel-head {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 16px; border-bottom: 1px solid #f0f0f0; background: #fafafa;
    min-height: 52px; flex-wrap: wrap; gap: 8px;
}
.panel-title-group { display: flex; align-items: center; margin-right: auto; }
.label { font-size: 0.9rem; font-weight: 500; color: var(--text-sub); }
.panel-actions { display: flex; gap: 4px; align-items: center; }

.editor-container { position: relative; flex: 1; display: flex; min-height: 200px; }
textarea {
    width: 100%; height: 100%; border: none; resize: vertical;
    padding: 16px; font-size: 18px; outline: none;
    background-color: transparent; line-height: 1.6;
    font-family: twhei-s, TWHEI, "台灣黑體", monospace; 
}
textarea::selection { background-color: #c2e7ff; color: #001d35; }
#font-output { 
    font-family: twhei-s, TWHEI, "台灣黑體", "WeSingMusic", "OikasuFont", monospace; 
    background-color: #f8fbff; 
}

/* ==========================================
   Controls & Buttons
   ========================================== */
.icon-btn, .icon-btn-medium, .icon-btn-small, .icon-btn-tiny {
    display: flex; align-items: center; justify-content: center;
    background: none; border: none; cursor: pointer; border-radius: 50%;
    color: var(--text-sub); transition: 0.2s; flex-shrink: 0;
}
.icon-btn { width: 40px; height: 40px; }
.icon-btn-medium { width: 32px; height: 32px; }
.icon-btn-medium.active { background-color: #d3e3fd; color: #041e49; font-weight: bold; transform: translateY(1px); box-shadow: inset 0 1px 2px rgba(0,0,0,0.1); }
.icon-btn-small { width: 28px; height: 28px; }
.icon-btn-tiny { width: 28px; height: 28px; margin-right: 4px; }
.icon-btn:hover, .icon-btn-medium:hover, .icon-btn-small:hover, .icon-btn-tiny:hover { background-color: #f0f4f9; color: var(--text-main); }

.text-btn {
    background: none; border: none; cursor: pointer;
    font-size: 0.85rem; color: var(--text-sub); font-weight: 500;
    padding: 6px 10px; border-radius: 18px; transition: 0.2s; white-space: nowrap;
}
.text-btn:hover { background-color: #fce8e6; color: #b3261e; }
.divider-vertical { width: 1px; height: 16px; background-color: var(--border); margin: 0 4px; }
.divider-vertical.small { height: 20px; background-color: #c7c7c7; }

/* Playback Group */
.playback-group { display: flex; align-items: center; position: relative; }
.action-btn {
    display: flex; align-items: center; justify-content: center;
    border: 1px solid var(--border); background: white; cursor: pointer;
    transition: 0.2s; height: 32px;
}
.play-main-btn {
    border-radius: 16px 0 0 16px; padding: 0 12px; border-right: none;
    color: #188038; background: #e6f4ea; border-color: #ceead6;
}
.play-main-btn:hover { background: #ceead6; color: #137333; }
.play-main-btn.playing { color: #d93025; background: #fce8e6; border-color: #fad2cf; }
.play-main-btn.playing:hover { background: #fad2cf; color: #b3261e; }

.settings-trigger-btn {
    border-radius: 0 16px 16px 0; padding: 0 8px;
    color: var(--text-sub); min-width: 32px; display: flex; justify-content: center;
}
.settings-trigger-btn:hover { background-color: #f0f4f9; }

.monochrome-icon { font-size: 1rem; filter: grayscale(100%) opacity(0.8); display: flex; align-items: center; justify-content: center; }
.icon-loading { width: 14px; height: 14px; border: 2px solid #188038; border-top-color: transparent; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { 100% { transform: rotate(360deg); } }

/* ==========================================
   Settings Popover
   ========================================== */
.settings-popover {
    display: none; position: absolute; top: 100%; left: auto; right: 0;
    margin-top: 8px; background: white; border: 1px solid var(--border);
    border-radius: 12px; padding: 0; box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    z-index: 50; min-width: 250px; width: 260px; overflow: hidden;
}
.settings-popover.show { display: block; }

.tabs-header { display: flex; background-color: #f0f4f9; border-bottom: 1px solid var(--border); }
.tab-btn {
    flex: 1; border: none; background: none; padding: 12px 0; font-size: 0.9rem;
    color: var(--text-sub); cursor: pointer; font-weight: 500; border-bottom: 2px solid transparent; transition: all 0.2s;
}
.tab-btn:hover { background-color: #e2e7eb; color: var(--text-main); }
.tab-btn.active { color: #0b57d0; border-bottom: 2px solid #0b57d0; background-color: #ffffff; }

.tab-content { display: none; padding: 16px; max-height: 300px; overflow-y: auto; }
.tab-content.active { display: block; animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.setting-section { margin-bottom: 16px; }
.setting-section:last-child { margin-bottom: 0; }
.separator-top { border-top: 1px solid #f0f0f0; padding-top: 12px; }
.section-label { font-size: 0.8rem; color: var(--text-sub); font-weight: 600; margin-bottom: 8px; }
.hint-text { font-size: 0.75rem; color: var(--text-sub); margin-top: 6px; text-align: center; }

.control-row { display: flex; align-items: center; gap: 8px; }
.stepper-btn { 
    width: 28px; height: 28px; border-radius: 50%; border: 1px solid var(--border); 
    background: white; cursor: pointer; display: flex; align-items: center; justify-content: center; 
    font-weight: bold; color: var(--text-sub); flex-shrink: 0;
}
.stepper-btn:hover { background: #f0f4f9; color: var(--text-main); }
.wide-btn { flex: 1; border-radius: 8px; font-weight: 500; font-size: 0.85rem; width: auto; height: 32px; color: #444746; gap: 4px; }
.action-transpose-btn { background-color: #f8f9fa; border-color: #e0e3e1; }
.action-transpose-btn:hover { background-color: #e8f0fe; color: #0b57d0; border-color: #d3e3fd; }
.action-transpose-btn span { font-weight: bold; margin-right: 4px; }

#tempo-input { width: 80px; text-align: center; border: 1px solid var(--border); border-radius: 4px; padding: 4px; font-weight: 500; font-size: 1rem; }
.wide-select { width: 100%; padding: 6px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.95rem; background: white; }

.transpose-row { justify-content: flex-start; }
.value-display { font-family: monospace; font-weight: 600; width: 30px; text-align: center; }
.key-result-wrapper { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.key-result-label { font-size: 0.8rem; color: var(--text-sub); }
.key-result { font-size: 0.9rem; color: #0b57d0; font-weight: 600; }

.instrument-list { display: flex; flex-direction: column; gap: 4px; padding-right: 4px; }
.inst-option { display: flex; align-items: center; justify-content: flex-start; padding: 10px 12px; margin-bottom: 2px; border-radius: 8px; cursor: pointer; gap: 8px; transition: 0.2s; }
.inst-option:hover { background-color: #f0f4f9; }
.inst-option.selected { background-color: #e8f0fe; color: #0b57d0; font-weight: 500; }
.inst-name { flex: 1; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inst-check { color: #0b57d0; font-weight: bold; width: 16px; text-align: center; flex-shrink: 0; display: none; }
.inst-option.selected .inst-check { display: block; }
.sub-label { display: none; }

/* ==========================================
   Virtual Keyboard & Replace Bar
   ========================================== */
.quick-toolbar { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; padding: 8px 12px; background-color: #f8f9fa; border-bottom: 1px solid var(--border); transition: all 0.3s ease; }
.quick-toolbar.hidden { display: none; }
.key-btn { height: 38px; display: flex; align-items: center; justify-content: center; background-color: #ffffff; border: 1px solid #dadce0; border-radius: 6px; font-family: monospace; font-size: 1rem; color: var(--text-main); cursor: pointer; user-select: none; box-shadow: 0 1px 1px rgba(0,0,0,0.05); padding: 0; min-width: 0; }
.key-btn:active { background-color: #e8f0fe; transform: translateY(1px); }
.key-btn.num-key { font-weight: 700; color: #0b57d0; font-size: 1.1rem; }
.key-btn.func-key { background-color: #f1f3f4; color: var(--text-sub); }
@media (min-width: 1024px) { .quick-toolbar { grid-template-columns: repeat(20, 1fr); padding: 10px 16px; } }

.replace-bar.single-line { display: flex; align-items: center; gap: 6px; padding: 6px 12px; background-color: #f0f4f9; border-bottom: 1px solid var(--border); animation: slideDown 0.2s ease-out; white-space: nowrap; overflow-x: auto; }
@keyframes slideDown { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }
.replace-input.tiny { width: 50px; padding: 4px 6px; border: 1px solid #d0d7de; border-radius: 4px; font-size: 0.85rem; text-align: center; }
.replace-input.tiny:focus { outline: none; border-color: #0b57d0; background-color: white; }
.arrow-icon { color: var(--text-sub); font-size: 0.9rem; font-weight: bold; user-select: none; }
.replace-confirm-btn.compact { padding: 4px 10px; font-size: 0.85rem; background-color: #0b57d0; color: white; border-radius: 4px; flex-shrink: 0; }
.replace-confirm-btn.compact:hover { background-color: #0a4ab8; }
.quick-actions-group { display: flex; align-items: center; gap: 2px; }
.quick-replace-btn { padding: 2px 0; width: 26px; height: 28px; font-family: monospace; font-weight: bold; font-size: 0.95rem; color: #444746; background-color: #ffffff; border: 1px solid #dadce0; border-radius: 4px; display: flex; align-items: center; justify-content: center; transition: 0.2s; }
.quick-replace-btn:hover { background-color: #e8f0fe; color: #0b57d0; border-color: #d3e3fd; }
.quick-replace-btn:active { background-color: #d2e3fc; transform: translateY(1px); }

/* ==========================================
   Help & Modals (說明與彈窗)
   ========================================== */
.footer-help { padding: 20px; }
.help-details { border: 1px solid var(--border); border-radius: var(--radius-md); background: #fdfdfd; }
.help-details summary { padding: 12px 16px; cursor: pointer; color: var(--text-sub); font-size: 0.9rem; list-style: none; display: flex; align-items: center; gap: 8px; font-weight: 500; }
.help-content { padding: 0 16px 16px 16px; }
.cheat-sheet { display: flex; flex-wrap: wrap; gap: 8px; }
.tag { background: #e8f0fe; color: #0b57d0; padding: 6px 12px; border-radius: 8px; font-size: 0.85rem; }
.help-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.help-category { background-color: #f8f9fa; padding: 12px; border-radius: 12px; border: 1px solid #e0e3e1; }
.help-category h5 { margin: 0 0 8px 0; font-size: 1rem; color: #0b57d0; border-bottom: 2px solid #e0e3e1; padding-bottom: 4px; }
.help-list { list-style: none; padding: 0; margin: 0; }
.help-list li { margin-bottom: 6px; display: flex; flex-wrap: wrap; align-items: baseline; }
.help-list code { background-color: #e8f0fe; color: #0b57d0; padding: 2px 6px; border-radius: 4px; font-family: monospace; font-weight: bold; margin-right: 6px; font-size: 0.9em; }
.help-section h4 { margin: 0 0 8px 0; color: #b3261e; }
.highlight-rule { font-weight: bold; color: #b3261e; background-color: #fce8e6; padding: 8px 12px; border-radius: 8px; margin: 0; }

.overlay, .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.4); z-index: 140; display: none; opacity: 0; transition: opacity 0.3s; }
.overlay.show, .modal-overlay.show { display: flex; opacity: 1; pointer-events: auto; }
.modal-overlay { z-index: 200; align-items: center; justify-content: center; }
.modal-box { background: white; width: 90%; max-width: 320px; padding: 24px; border-radius: 28px; box-shadow: 0 4px 20px rgba(0,0,0,0.15); transform: scale(0.9); transition: transform 0.2s cubic-bezier(0.2, 0, 0, 1); }
.modal-overlay.show .modal-box { transform: scale(1); }
.modal-title { margin: 0 0 12px 0; font-size: 1.2rem; color: var(--text-main); }
.modal-message { margin: 0 0 24px 0; color: var(--text-sub); line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: 12px; }
.filled-btn-danger { background-color: #b3261e; color: white; border: none; padding: 10px 24px; border-radius: 20px; font-weight: 500; cursor: pointer; transition: background 0.2s; }
.filled-btn-danger:hover { background-color: #8c1d18; }

/* --- Rhythm Dictionary Large Modal --- */
.large-modal { max-width: 800px; width: 95%; height: 80vh; display: flex; flex-direction: column; padding: 0; overflow: hidden; border-radius: 12px; }
.large-modal .modal-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; background-color: #f8f9fa; }
.large-modal .modal-controls { padding: 12px 20px; background-color: #fff; border-bottom: 1px solid var(--border); display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.control-group { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; color: var(--text-sub); }
.compact-select { padding: 4px 8px; border-radius: 6px; border: 1px solid var(--border); font-family: inherit; background-color: white; }
.tab-group { display: flex; background: #f0f4f9; border-radius: 8px; padding: 2px; }
.filter-tab { padding: 4px 12px; border: none; background: none; cursor: pointer; font-size: 0.85rem; border-radius: 6px; color: var(--text-sub); transition: 0.2s; }
.filter-tab.active { background: white; color: #0b57d0; font-weight: bold; box-shadow: 0 1px 2px rgba(0,0,0,0.1); }
.rhythm-list-container { flex: 1; overflow-y: auto; padding: 0; background-color: #fff; }
.rhythm-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.rhythm-table th { background: #f8f9fa; padding: 10px 16px; text-align: left; position: sticky; top: 0; border-bottom: 1px solid var(--border); color: var(--text-sub); font-weight: 600; z-index: 10; }
.rhythm-table td { padding: 10px 16px; border-bottom: 1px solid #eee; vertical-align: middle; }
.rhythm-table tr:hover { background-color: #f8fbff; }
.note-view { font-family: monospace; color: #0b57d0; font-size: 0.95rem; letter-spacing: 0.5px; }
.play-sample-btn { width: 32px; height: 32px; border-radius: 50%; border: 1px solid #e0e3e1; background: white; color: #188038; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: 0.2s; }
.play-sample-btn:hover { background-color: #e6f4ea; border-color: #188038; }

/* ==========================================
   Media Queries (Mobile Response)
   ========================================== */
@media (max-width: 768px) {
    .app-name { font-size: 0.9rem; }
    .app-text { display: none; }
    .top-bar { padding: 0 10px; }
    .header-center { max-width: 160px; }
    .sidebar.collapsed { margin-left: 0; }
    .sidebar { position: fixed; left: 0; top: 0; bottom: 0; transform: translateX(-100%); margin-left: 0; box-shadow: 4px 0 12px rgba(0,0,0,0.15); height: 100%; width: 260px; background-color: white; }
    .sidebar.open { transform: translateX(0); }
    .mobile-sidebar-title { display: block; }
    .workspace { flex-direction: column !important; padding: 0 10px; }
    .panel-actions { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
    .action-btn, .icon-btn-medium { flex-shrink: 0; }
    .delete-song-btn { opacity: 1 !important; color: #999; padding: 10px; }
    .delete-song-btn:active { color: #b3261e; background-color: rgba(0,0,0,0.05); }
    .settings-popover { position: fixed; top: 50% !important; left: 50% !important; transform: translate(-50%, -50%); margin-top: 0; width: 80%; max-width: 300px; z-index: 1000; box-shadow: 0 4px 20px rgba(0,0,0,0.3); max-height: 80vh; overflow-y: auto; }
}

@media (max-width: 600px) {
    /* --- 手機版節奏字典 (格狀/卡片式排版) --- */
    .rhythm-table, .rhythm-table tbody { display: block; }
    .rhythm-table thead { display: none; } /* 隱藏表頭 */
    
    .rhythm-table tr {
        display: grid;
        /* 定義網格： 
           Col 1: 代號 (自動緊縮) 
           Col 2: 名稱 (自動填滿) 
           Col 3: 按鈕 (固定寬)
        */
        grid-template-columns: max-content 1fr auto; 
        grid-template-rows: auto auto; /* 兩列高度 */
        column-gap: 8px; /* 代號與名稱的間距 */
        row-gap: 4px;    /* 名稱與音符的間距 */
        padding: 12px 16px;
        border-bottom: 1px solid #eee;
        align-items: center; /* 垂直置中 */
    }

    /* 代號 (左上) */
    .rhythm-table td:nth-child(1) {
        grid-column: 1 / 2; grid-row: 1;
        padding: 0; border: none;
        font-weight: bold; color: #0b57d0; font-size: 0.95rem;
    }
    
    /* 名稱 (中上) */
    .rhythm-table td:nth-child(2) {
        grid-column: 2 / 3; grid-row: 1;
        padding: 0; border: none;
        font-weight: normal; color: var(--text-main); font-size: 0.95rem;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
        display: block; /* 確保 ellipsis 生效 */
    }

    /* 播放按鈕 (右上) */
    .rhythm-table td:nth-child(4) {
        grid-column: 3 / 4; grid-row: 1; 
        padding: 0; border: none;
        justify-self: end;
    }

    /* 音符檢視 (下方跨欄，從第 2 欄開始對齊名稱) */
    .rhythm-table td:nth-child(3) {
        grid-column: 2 / -1; /* 從第 2 欄跨到最後，實現與名稱對齊 */
        grid-row: 2; 
        padding: 4px 0 0 0; border: none;
        font-size: 0.9rem; color: #0b57d0;
        white-space: nowrap; overflow-x: auto; /* 若太長可滑動 */
        font-family: monospace;
    }

    /* --- 手機版控制項 (單行強制優化) --- */
    .large-modal .modal-controls { 
        display: flex;
        flex-direction: row; /* 強制水平排列 */
        flex-wrap: nowrap;   /* 禁止換行 */
        gap: 8px; padding: 10px 12px; 
        overflow-x: auto;    /* 防止小螢幕溢出 */
        align-items: center;
        justify-content: space-between;
    }
    
    .control-group { 
        display: flex; align-items: center;
        gap: 4px; white-space: nowrap; flex-shrink: 0; 
    }
    
    /* 縮小手機版標籤文字 */
    .control-group label { font-size: 0.85rem; margin: 0; }
    
    .compact-select { 
        padding: 4px 24px 4px 8px; font-size: 0.85rem; width: auto;
    }
    
    .tab-group { display: flex; flex-shrink: 0; }
    .filter-tab { padding: 4px 10px; font-size: 0.8rem; }
}

/* 1. 預設狀態 (電腦版/大螢幕)：隱藏 */
#keyboard-toggle-btn {
    display: none;
}

/* 2. 行動裝置狀態 (手機/平板)：顯示 */
/* max-width: 768px 代表螢幕寬度在 768px 以下時生效 (這是常見的平板直向/手機界線) */
@media (max-width: 768px) {
    #keyboard-toggle-btn {
        /* 使用 flex 以保持與其他 icon-btn 排版一致 */
        display: flex; 
        
        /* 確保按鈕不會太小，好點擊 */
        align-items: center;
        justify-content: center;
    }
}

/* 當鍵盤開啟時的樣式 (可選) */
#keyboard-toggle-btn.active {
    color: #0b57d0; /* 使用您的主色系 */
}


/* ==========================================
   Virtual Keyboard (虛擬鍵盤)
   ========================================== */
.quick-toolbar { 
    display: grid; 
    /* 預設模式 (固定)：電腦版單行，手機版雙行 (由 Media Query 控制) */
    grid-template-columns: repeat(10, 1fr); 
    gap: 4px; padding: 8px 12px; 
    background-color: #f8f9fa; 
    border-bottom: 1px solid var(--border); 
    transition: transform 0.3s ease, opacity 0.3s ease; 
}

/* 隱藏狀態 */
.quick-toolbar.hidden { 
    display: none; 
}

/* ==========================================
   1. 固定鍵盤 (原有的 quick-toolbar)
   ========================================== */
.quick-toolbar { 
    display: grid; 
    grid-template-columns: repeat(10, 1fr); 
    gap: 4px; padding: 8px 12px; 
    background-color: #f8f9fa; 
    border-bottom: 1px solid var(--border); 
}
.quick-toolbar.hidden { display: none; }

/* ==========================================
   2. 浮動鍵盤 (全新的 Floating Keyboard)
   ========================================== */
.floating-keyboard {
    position: fixed;

	bottom: 20px; 
    left: 50%;                   /* 1. 設定左側基準點為螢幕的一半 */
    transform: translateX(-50%); /* 2. 向左移動自身寬度的一半，達成完全居中 */
    right: auto;

    width: 95%; max-width: 400px; /* 手機寬度友善，電腦不超過 400px */
    background-color: rgba(255, 255, 255, 0.95);
    border: 1px solid #ccc;
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.25);
    backdrop-filter: blur(8px);
    z-index: 9999; /* 確保在最上層 */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: opacity 0.2s, transform 0.2s;
}

.floating-keyboard.hidden {
    display: none;
}

/* 拖曳手把 */
.drag-handle {
    height: 36px;
    background-color: #f1f3f4;
    border-bottom: 1px solid #e0e0e0;
    cursor: move; /* 游標提示可拖曳 */
    display: flex;
    align-items: center;
    justify-content: center;
    color: #888;
    font-size: 12px;
    font-weight: bold;
    user-select: none;
    position: relative;
}

.close-float-btn {
    position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
    background: none; border: none; font-size: 16px; cursor: pointer; color: #666;
    width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
}
.close-float-btn:hover { background-color: #ddd; color: #000; }

/* 浮動鍵盤的按鈕網格 */
.keys-grid {
    display: grid;
    /* [關鍵] 強制 8 列 (約 3 行)，確保按鈕夠大 */
    grid-template-columns: repeat(10, 1fr);
    gap: 6px;
    padding: 10px 10px;
}

/* 浮動鍵盤內的按鈕樣式 (繼承 key-btn 但微調) */
.keys-grid .key-btn {
    height: 36px; /* 比固定版高一點，更好按 */
    font-size: 1.1rem;
    border-radius: 8px;
    box-shadow: 0 2px 0 rgba(0,0,0,0.05);
}

/* 手機版優化：預設居中 */
@media (max-width: 768px) {
    .floating-keyboard {
        left: 50%; transform: translateX(-50%);
        bottom: 30px; right: auto;
    }
}

/* 切換模式按鈕 (進入其他模式) */
.key-btn.mode-btn {
    background-color: #e8f0fe;
    color: #0b57d0;
    font-weight: bold;
    font-size: 0.9rem;
    border-color: #d2e3fc;
}

/* 返回主鍵盤按鈕 */
.key-btn.return-btn {
    background-color: #fce8e6;
    color: #c5221f;
    font-weight: bold;
    border-color: #fad2cf;
}


/* ==========================================
   手機版優化：將固定鍵盤移至底部 (Overlay)
   ========================================== */
@media (max-width: 768px) {
    
    /* 針對固定鍵盤 (quick-toolbar) 進行定位覆寫 */
    .quick-toolbar {
        position: fixed;
        bottom: 0; 
        left: 5px; 
        right: 5px;
        width: 100%;        
        z-index: 150; 
        
        background-color: #f8f9fa;
        border-top: 1px solid #ccc;
        border-bottom: none;
        box-shadow: 0 -4px 16px rgba(0,0,0,0.15);
        
        /* iPhone X+ 底部安全區域 (避免 Home Bar 擋住按鈕) */
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        padding-top: 12px;
        
        /* 確保按鈕好按 */
        gap: 6px;
    }

    .editor-container textarea {
        padding-bottom: 220px !important; 
    }
}


/* 1. 讓浮動鍵盤內的所有按鈕字體稍微小一點 (原本是 1.1rem) */
.key-btn {
    font-size: 1rem; 
    padding: 0 2px; /* 減少左右內距，爭取空間 */
}

/* 2. 特別針對「和弦性質」按鈕 (如 maj7, sus4, add9) 進行壓縮 */
.key-btn.chord-quality {
    font-size: 0.75rem !important;
    letter-spacing: -0.5px;
    font-weight: 500;
    font-family: "Segoe UI", Roboto, sans-serif;
}


.key-btn.snippet-key {
    font-size: 0.75rem !important;
    letter-spacing: -0.5px;
    font-weight: 500;
    font-family: "Segoe UI", Roboto, sans-serif;
    color: #7b1fa2;            /* 深紫色文字 */
}

/* 按下時的回饋感 */
.keys-grid .key-btn.snippet-key:active {
    background-color: #e1bee7;
    transform: translateY(1px);
}

@media (max-width: 400px) {
    .keys-grid {
        /* 如果螢幕真的很小，稍微縮小間距 */
        gap: 3px; 
        padding: 8px;
    }
}



/* 1. Shift 鍵樣式 */
.key-btn.shift-btn {
    font-weight: bold;
    color: #5f6368;
    background-color: #f1f3f4;
}

/* Shift 啟用狀態 (亮燈) */
.key-btn.shift-btn.active {
    background-color: #c2e7ff; /* 淺藍色高亮 */
    color: #001d35;
    border-color: #7fcfff;
}

/* 2. 跨欄位工具 (Grid Spanning) 
   目前的 .keys-grid 是 repeat(10, 1fr)
   這些 class 讓按鈕可以佔據多個格子
*/
.key-btn.span-2 {
    grid-column: span 2;
}
.key-btn.span-3 {
    grid-column: span 3;
}
.key-btn.span-4 {
    grid-column: span 4;
}
.key-btn.span-5 {
    grid-column: span 5;
}
.key-btn.span-6 {
    grid-column: span 6;
}

/* 3. 空白鍵樣式 */
.key-btn.space-btn {
    font-size: 0.9rem;
    color: #666;
}

/* 4. 手機版微調
   如果手機版按鈕太小，可以適度縮小 QWERTY 模式下的 padding
*/
@media (max-width: 400px) {
    .layout-qwerty .key-btn {
        padding: 0;
        font-size: 0.9rem;
    }
}

/* ==========================================
   字體大小控制 (Font Size Control)
   ========================================== */

/* 預設 (Level 0) - 若沒有加 class 就是這個大小，或由原本 CSS 決定 */
textarea.fs-level-0 {
    font-size: 1.2rem !important;
    line-height: 1.5;
}

textarea.fs-level-1 {
    font-size: 1.25rem !important;
    line-height: 1.5;
}

textarea.fs-level-2 {
    font-size: 1.3rem !important;
    line-height: 1.5;
}

textarea.fs-level-3 {
    font-size: 1.4rem !important;
    line-height: 1.5;
}