:root {
    --bg: #0e1116;
    --bg-soft: #151a22;
    --chrome-h: 0px;
    --panel: #1a212c;
    --panel-2: #141a24;
    --line: #263041;
    --text: #e6edf6;
    --muted: #8b98ab;
    --accent: #4ea1ff;
    --accent-2: #35d0a5;
    --warn: #ffb454;
    --danger: #ff6b6b;
    --radius: 10px;
    --bw: clamp(15px, 4.2vw, 26px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
    font-size: 14px;
    -webkit-text-size-adjust: 100%;
}

body {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    overscroll-behavior: none;
}

body.dropping { outline: 2px dashed var(--accent); outline-offset: -6px; }

button, select, input { font-family: inherit; font-size: 13px; color: var(--text); }

button {
    background: #222c3a;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 7px 12px;
    cursor: pointer;
    min-height: 34px;
    -webkit-tap-highlight-color: transparent;
}
button:hover { background: #2b3646; }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06121f; font-weight: 600; padding: 12px 22px; font-size: 15px; }
button.danger { border-color: #5a2b2b; color: #ffb3b3; }
button.mini { padding: 5px 10px; border-radius: 6px; min-height: 30px; }

input[type="range"] { accent-color: var(--accent); height: 28px; }
select, input[type="text"] { background: #131923; border: 1px solid var(--line); border-radius: 6px; padding: 6px; }

/* ---------------- 启动遮罩 ---------------- */
.overlay {
    position: fixed; inset: 0; z-index: 50;
    background: radial-gradient(1200px 600px at 50% -10%, #1b2534 0%, #0b0e13 60%);
    display: flex; align-items: center; justify-content: center;
    padding: 24px;
}
.overlay.hidden { display: none; }
.overlay-box { text-align: center; max-width: 520px; width: 100%; }
.overlay-logo { font-size: 46px; color: var(--accent-2); line-height: 1; margin-bottom: 6px; }
.overlay-box h1 { margin: 0 0 8px; font-size: 26px; letter-spacing: .5px; }
.overlay-sub { color: var(--muted); margin: 0 0 26px; font-size: 13px; line-height: 1.6; }
.overlay-box .tip { color: var(--muted); font-size: 12px; margin-top: 18px; line-height: 1.6; }

/* ---------------- 顶栏 ---------------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 14px; padding-left: calc(14px + var(--safe-l)); padding-right: calc(14px + var(--safe-r));
    background: var(--bg-soft); border-bottom: 1px solid var(--line);
    gap: 10px; flex: none;
}

/* Electron：顶栏兼标题栏，隐藏系统菜单栏 */
html.electron {
    --chrome-h: 36px;
}
html.electron .topbar {
    min-height: var(--chrome-h);
    padding-top: 4px;
    padding-bottom: 4px;
    -webkit-app-region: drag;
}
html.electron .topbar button,
html.electron .topbar .status,
html.electron .topbar .brand { -webkit-app-region: no-drag; }
html.electron.platform-win32 .topbar {
    padding-right: calc(138px + var(--safe-r));
}
html.electron.platform-darwin .topbar {
    padding-left: calc(78px + var(--safe-l));
}
html.electron .brand-name { font-size: 13px; font-weight: 600; }
html.electron .version { font-size: 11px; margin-left: 2px; }
html.electron .tabs { padding-top: 4px; padding-bottom: 4px; }
html.electron .tab { padding-top: 6px; padding-bottom: 6px; min-height: 32px; }

/* Android 沉浸全屏：内容进安全区 */
html.native-app .topbar {
    padding-top: calc(6px + env(safe-area-inset-top, 0px));
}
html.native-app.platform-android[data-layout="phone"][data-orient="portrait"] .tabs {
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; min-width: 0; }
.logo { color: var(--accent-2); font-size: 20px; flex: none; }
.brand-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.version { color: var(--muted); font-size: 12px; font-weight: 400; white-space: nowrap; }
.top-controls { display: flex; align-items: center; gap: 10px; flex: none; min-width: 0; flex-wrap: wrap; justify-content: flex-end; }
.top-stats {
    display: flex; align-items: center; gap: 12px; flex: none;
    color: var(--muted); font-size: 12px; white-space: nowrap;
}
.top-stats b { color: var(--text); font-weight: 600; }
.status { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.slider { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); }
.slider.grow { flex: 1; min-width: 0; }
.slider input[type="range"] { flex: 1; min-width: 60px; }
.ok { color: var(--accent-2); }
.warn { color: var(--warn); }
.error { color: var(--danger); }

/* ---------------- 分页 ---------------- */
.tabs {
    display: flex; gap: 4px; padding: 6px 10px; background: var(--bg-soft);
    border-bottom: 1px solid var(--line); flex: none;
    overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
    position: relative; flex: 1 1 0; min-width: 72px;
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    background: transparent; border: 1px solid transparent; color: var(--muted);
    padding: 8px 10px; border-radius: 8px; font-size: 13px;
}
.tab:hover { background: #1d2734; }
.tab.active { background: #1d2b3d; border-color: #2c4a6b; color: var(--text); }
.tab .ico { font-size: 15px; }
.tab .badge {
    font-style: normal; font-size: 11px; line-height: 16px; min-width: 16px; text-align: center;
    background: var(--accent); color: #06121f; border-radius: 9px; padding: 0 5px;
    display: none;
}
.tab .badge.show { display: inline-block; }

main { flex: 1; min-height: 0; display: flex; padding: 12px; padding-left: calc(12px + var(--safe-l)); padding-right: calc(12px + var(--safe-r)); }
.tab-pane { display: none; flex: 1; min-height: 0; min-width: 0; flex-direction: column; gap: 10px; }
.tab-pane.active { display: flex; }

.pane-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex: none; }
.pane-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.pane-head .hint { margin: 0; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0; line-height: 1.5; }
.foot-hint { margin-top: auto; }

.panel {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px;
    min-height: 0; display: flex; flex-direction: column; gap: 8px;
}
.panel h2 { margin: 0; font-size: 14px; font-weight: 600; }
.panel.grow { flex: 1 1 0; min-height: 0; overflow: hidden; }
#pane-more > .panel:not(.grow) { max-width: 720px; }
.rec-box { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; padding-top: 8px; border-top: 1px solid var(--line); }
.rec-box h2 { margin: 0; font-size: 14px; }

/* 设置页横屏：左侧参数、右侧日志 */
html[data-orient="landscape"] #pane-more {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: stretch;
    gap: 12px;
}
html[data-orient="landscape"] #pane-more > .panel:not(.grow) {
    flex: 0 0 min(400px, 38%);
    max-width: none;
    min-width: 280px;
}
html[data-orient="landscape"] #pane-more > .panel.grow {
    flex: 1 1 0;
    min-width: 0;
    min-height: 0;
}

/* ---------------- 演奏页 ---------------- */
.play-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; flex: none; }
.play-bar.bottom { gap: 14px; }
.octave-ctrl { display: flex; align-items: center; gap: 8px; flex: none; }
.octave-ctrl .mini { min-width: 4.2em; }
.play-target { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.play-target .lbl { color: var(--muted); font-size: 12px; flex: none; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
    flex: none; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--line);
    background: #161d27; color: var(--muted); font-size: 12px; min-height: 30px; white-space: nowrap;
}
.chip.on { background: rgba(78, 161, 255, .18); border-color: var(--accent); color: var(--text); }
.chip.off { opacity: .5; }

.piano-wrap { flex: 1; min-height: 0; display: flex; align-items: flex-end; }

/*
 * 竖屏三排高度由 layout.js → --jianpu-total-h（宽/7×4.2×3+间距，且 ≤ slot 高）。
 * 勿对 .piano.jianpu 使用 tablet/phone 的 .piano { height:… } 规则。
 */
.piano-wrap:has(.piano.jianpu) {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: stretch;
    overflow: hidden;
}
#pane-play .play-bar.bottom {
    flex: none;
    position: relative;
    z-index: 2;
}
.piano-wrap:has(.piano.jianpu) + .piano-hint {
    margin-top: 8px;
}
html[data-orient="portrait"] #pane-play {
    min-height: 0;
    overflow: hidden;
}

.piano:not(.jianpu),
.piano-bar {
    position: relative;
    width: 100%;
    display: flex;
    user-select: none;
    touch-action: none;
    padding-bottom: 2px;
}
.piano:not(.jianpu) {
    height: clamp(110px, 26vh, 240px);
}
html[data-orient="portrait"] .piano:not(.jianpu) { height: clamp(110px, 26vh, 240px); }

.piano.jianpu {
    --row-gap: 14px;
    --bk-of-white: 0.58;
    width: 100%;
    flex: none;
    /* layout.js 变量未就绪时避免三排高度为 0（看起来像空白） */
    min-height: clamp(100px, 28vh, 360px);
    height: var(--jianpu-total-h, auto);
    max-height: var(--jianpu-slot-h, 100%);
    display: grid;
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: var(--row-gap);
}
.piano.jianpu > .piano-bar {
    height: 100%;
    min-height: 0;
    --bw: calc(100% / 7 * var(--bk-of-white));
}
.pkey {
    position: relative; border-radius: 0 0 5px 5px; cursor: pointer;
    display: flex; align-items: flex-end; justify-content: center;
    touch-action: none; -webkit-tap-highlight-color: transparent;
}
.pkey .pk-label { font-size: 9px; color: #6b7484; padding-bottom: 3px; pointer-events: none; white-space: nowrap; line-height: 1.1; }
.pkey.white {
    flex: 1;
    min-width: 0;
    align-self: stretch;
    background: linear-gradient(#f6f8fb, #d7dde6);
    border: 1px solid #b9c2ce;
}
.pkey.white.active { background: linear-gradient(#9fd0ff, #6bb4f5); }
.pkey.black {
    position: absolute; top: 0; width: var(--bw); height: 62%;
    background: linear-gradient(#2c3441, #12161d); border: 1px solid #000; z-index: 2; color: #fff;
    border-radius: 0 0 4px 4px;
}
.pkey.black .pk-label { color: #b8c4d4; font-size: 7px; padding-bottom: 2px; }
.pkey.black.active { background: linear-gradient(#3d7cc0, #1d4e86); }
.pkey.black.active .pk-label { color: #eef6ff; }
.piano-hint { margin: 0; flex: none; }

/* ---------------- 手柄页 ---------------- */
#pane-devices .device-list { flex: 1; min-height: 0; }
.device-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 10px; overflow: auto; align-content: start; padding-bottom: 4px; }
.device-foot {
    flex: none; margin-top: auto; padding-top: 10px;
    display: flex; flex-direction: column; gap: 8px; align-items: stretch;
}
.device-foot .scan-btn { width: 100%; min-height: 44px; font-size: 15px; }
.device-foot .hint { margin: 0; text-align: center; }

.card {
    border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); padding: 10px;
    display: flex; flex-direction: column; gap: 8px;
}
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(78, 161, 255, .35) inset; }
.card.offline { opacity: .65; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.card-title { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.card-title .label-input {
    background: transparent; border: 1px solid transparent; border-radius: 5px;
    padding: 4px; font-weight: 600; min-width: 0; flex: 1; font-size: 14px;
}
.card-title .label-input:hover, .card-title .label-input:focus { border-color: var(--line); outline: none; }
.card-actions { display: flex; align-items: center; gap: 6px; flex: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; display: inline-block; }
.dot-on { background: var(--accent-2); }
.dot-off { background: #5a6473; }
.dot.flash { background: #fff; box-shadow: 0 0 8px 2px var(--accent); }
.tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; flex: none; }
.tag-ble { color: #7fd0ff; border-color: #2a4c66; }
.tag-mac { font-size: 10px; font-variant-numeric: tabular-nums; opacity: .88; max-width: 9em; overflow: hidden; text-overflow: ellipsis; }
.tag-warn { color: var(--warn); border-color: #57452b; }
.tag-off { color: var(--warn); border-color: #57452b; }
.card-body { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.row > label { width: 66px; color: var(--muted); flex: none; font-size: 12px; }
.row > label.chk, .row > label.switch { width: auto; }
.row select { flex: 1; min-width: 0; }
.row input[type="range"] { flex: 1; min-width: 0; }
.row .val { width: 46px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; flex: none; font-size: 12px; }
.row-toggles { gap: 14px; flex-wrap: wrap; }
.chk { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.chk input { width: 16px; height: 16px; }
.chk.inline { flex: none; }
.switch { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.switch input { width: 16px; height: 16px; }
.card-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); padding-top: 6px; gap: 8px; }
.card-foot .hint { margin: 0; }
.activity { font-size: 12px; color: var(--accent-2); min-height: 16px; text-align: right; }
.activity.on { color: #fff; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

/* ---------------- 音源页 ---------------- */
.bank-list { display: flex; flex-direction: column; gap: 6px; overflow: auto; }
.bank-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.bank-item.is-ignored { opacity: 0.72; border-style: dashed; }
.bank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bank-ops { display: flex; gap: 4px; flex: none; flex-wrap: wrap; justify-content: flex-end; }
.tag-muted { color: var(--muted); border-color: var(--muted); }

/* ---------------- 日志 ---------------- */
.log {
    flex: 1 1 auto;
    height: 0;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    font-size: 12px;
    line-height: 1.6;
}
.log-line { border-bottom: 1px dotted #202836; padding: 3px 0; color: var(--muted); word-break: break-word; }
.log-ok { color: var(--accent-2); }
.log-warn { color: var(--warn); }
.log-error { color: var(--danger); }

/* ---------------- 桌面（Electron 默认 ≥1024） ---------------- */
html[data-layout="desktop"] .piano:not(.jianpu) { height: clamp(140px, 24vh, 260px); }
html[data-layout="desktop"] .device-list { grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); }

/* ---------------- 平板（600–1023，含 Android 平板） ---------------- */
html[data-layout="tablet"] .piano:not(.jianpu) { height: clamp(150px, 28vh, 300px); }
html[data-layout="tablet"] .device-list { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
html[data-layout="tablet"] main { padding: 12px 16px; }
html[data-layout="tablet"] .tab { min-height: 40px; }
html[data-layout="tablet"][data-orient="landscape"] .device-list {
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
}
html[data-layout="tablet"][data-orient="portrait"] #pane-more > .panel:not(.grow) { max-width: none; }

/* ---------------- 手机（<600） ---------------- */
html[data-layout="phone"] {
    font-size: 14px;
}
html[data-layout="phone"] .brand-name { display: none; }
html[data-layout="phone"] .topbar {
    padding: 8px 12px;
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
}
html[data-layout="phone"] .tabs { padding: 6px 8px; gap: 2px; }
html[data-layout="phone"] .tab { min-width: 56px; font-size: 12px; padding: 10px 6px; min-height: 44px; }
html[data-layout="phone"] .tab .ico { font-size: 18px; }
html[data-layout="phone"] main { padding: 10px; }
html[data-layout="phone"] .piano:not(.jianpu) { height: clamp(128px, min(40vh, 42dvh), 260px); }
html[data-layout="phone"] .device-list { grid-template-columns: 1fr; }
html[data-layout="phone"] .row > label { width: 58px; }
html[data-layout="phone"] .row .val { width: 42px; }
html[data-layout="phone"] .top-stats { gap: 8px; font-size: 10px; }
html[data-layout="phone"] .overlay-box h1 { font-size: 22px; }
html[data-layout="phone"] .overlay-box .tip { font-size: 11px; }
html[data-layout="phone"] .play-bar.bottom { flex-wrap: nowrap; }
html[data-layout="phone"] .play-bar.bottom .slider.grow { min-width: 0; }
/* 手机竖屏：底 Tab（拇指区） */
html[data-layout="phone"][data-orient="portrait"] .tabs {
    order: 3;
    border-top: 1px solid var(--line);
    border-bottom: none;
    padding-top: 8px;
    padding-bottom: calc(8px + var(--safe-b));
    background: #121821;
    box-shadow: 0 -4px 24px rgba(0, 0, 0, .35);
}
html[data-layout="phone"][data-orient="portrait"] .tab .txt { display: none; }
html[data-layout="phone"][data-orient="portrait"] main { order: 2; padding-bottom: 4px; }

/* 手机横屏：压缩顶栏，钢琴尽量占宽 */
html[data-layout="phone"][data-orient="landscape"] .tabs { padding: 4px 8px; }
html[data-layout="phone"][data-orient="landscape"] .piano:not(.jianpu) { height: clamp(100px, 52vh, 200px); }
html[data-layout="phone"][data-orient="landscape"] main { padding: 6px 10px; }
html[data-layout="phone"][data-orient="landscape"] #pane-play .play-bar:first-child { display: none; }
/* ---------------- 触摸设备（Capacitor / 触屏） ---------------- */
html[data-layout="phone"] button,
html[data-layout="tablet"] button,
body.touch-ui button { min-height: 40px; }
html[data-layout="phone"] button.mini,
html[data-layout="tablet"] button.mini,
body.touch-ui button.mini { min-height: 36px; padding: 6px 12px; }
body.touch-ui input[type="range"] { height: 32px; }
body.touch-ui .chip { min-height: 36px; padding: 8px 14px; }

/* 无 JS 时的 media 回退（首屏闪动前） */
@media (max-width: 599px) {
    .brand-name { display: none; }
}
