/* ---------- 主題 ---------- */
:root {
  --bg: #fbfaf8;
  --panel: #ffffff;
  --border: #e3ded6;
  --text: #23201c;
  --muted: #7d766c;
  --chord: #c2410c;
  --accent: #1d6f5c;
  --role-m: #1d4ed8;
  --role-f: #be185d;
  --role-b: #6d28d9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 8px 24px rgba(0, 0, 0, .04);
  --radius: 10px;
  --sheet-size: 18px;
}

:root[data-theme="dark"] {
  --bg: #17161a;
  --panel: #1f1e23;
  --border: #34323a;
  --text: #eceaf0;
  --muted: #9a95a3;
  --chord: #fb923c;
  --accent: #5eead4;
  --role-m: #7dabff;
  --role-f: #f9a8d4;
  --role-b: #c4b5fd;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

a { color: inherit; }

/* ---------- 版面 ---------- */
.wrap { max-width: 900px; margin: 0 auto; padding: 0 16px 96px; }
/* 編輯頁是表單＋預覽兩欄，用看譜頁的窄版會擠成一團、兩側大片留白 */
body[data-view="editor"] .wrap { max-width: 1500px; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 900px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px;
}
.brand { font-weight: 700; font-size: 17px; text-decoration: none; letter-spacing: .02em; }
.brand span { color: var(--accent); }
.topbar .spacer { flex: 1; }

.login-pane { max-width: 380px; margin: 48px auto; }
.login-pane h2 { margin: 0 0 16px; font-size: 16px; }
.login-pane input { width: 100%; }

.draft-banner {
  background: color-mix(in srgb, var(--chord) 12%, var(--bg));
  border-bottom: 1px solid var(--border);
  color: var(--chord);
  font-size: 13px;
  padding: 7px 16px;
  text-align: center;
}

/* ---------- 控制元件 ---------- */
button, select, input, textarea {
  font: inherit; color: inherit;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
:root[data-theme="dark"] button.primary { color: #10201c; }
button.ghost { background: transparent; }
button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] button.on { color: #10201c; }
button.icon { padding: 6px 9px; line-height: 1; }
textarea { width: 100%; resize: vertical; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--muted); }

/* ---------- 歌曲列表 ---------- */
.search-row { display: flex; gap: 8px; margin: 20px 0 8px; }
.search-row input { flex: 1; }
.count { color: var(--muted); font-size: 13px; margin: 0 0 12px; }

.song-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.song-list a {
  display: flex; align-items: baseline; gap: 10px;
  padding: 12px 14px; text-decoration: none;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.song-list a:hover { border-color: var(--accent); }
.song-list .t { font-weight: 600; }
.song-list .a { color: var(--muted); font-size: 14px; }
.song-list .g {
  font-size: 11px; font-weight: 700; line-height: 1.5;
  padding: 0 .4em; border-radius: 4px; border: 1px solid currentColor;
}
.song-list .g.role-m { color: var(--role-m); }
.song-list .g.role-f { color: var(--role-f); }
.song-list .g.role-b { color: var(--role-b); }

.song-list a.unpublished { opacity: .55; border-style: dashed; }
.song-list .g.down { color: var(--muted); }

.filter-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.filter-row select { max-width: 100%; }
.song-list .k { margin-left: auto; color: var(--chord); font-size: 13px; font-weight: 600; white-space: nowrap; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }

/* ---------- 看譜工具列 ---------- */
.song-head { padding: 18px 0 4px; }
.song-head h1 { margin: 0 0 4px; font-size: 24px; }
.song-head .meta { color: var(--muted); font-size: 14px; display: flex; flex-wrap: wrap; gap: 4px 14px; }

.toolbar {
  position: sticky; top: 49px; z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  margin: 12px -16px 0; padding: 10px 16px;
  display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center;
}
.tool-extra { display: contents; }
.more-btn { display: none; }
.tool { display: flex; align-items: center; gap: 6px; }
.tool > .lbl { font-size: 12px; color: var(--muted); }
.keybox {
  min-width: 58px; text-align: center; font-weight: 700;
  color: var(--chord); padding: 4px 8px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--panel);
}
.sound-hint { font-size: 12px; color: var(--muted); white-space: nowrap; }
.hint { font-size: 12px; color: var(--muted); }
.hint b { color: var(--chord); }

/* ---------- 譜面 ---------- */
.sheet { font-size: var(--sheet-size); padding: 20px 0 40px; }
.sheet .blank { height: .8em; }
.sheet .section {
  margin: 22px 0 8px; font-size: .82em; letter-spacing: .08em;
  color: var(--accent); text-transform: uppercase;
  border-bottom: 1px solid var(--border); padding-bottom: 4px;
}
.sheet .note { margin: 6px 0; font-size: .82em; color: var(--muted); }

.line {
  display: flex; flex-wrap: wrap; align-items: flex-end;
  margin-bottom: .45em; min-height: 1.2em;
}
.line.dimmed { opacity: .35; }
.role-tag {
  align-self: flex-end; flex: none;
  font-size: .6em; font-weight: 700; line-height: 1.6;
  padding: 0 .45em; margin-right: .5em; border-radius: 4px;
  border: 1px solid currentColor;
}
.role-tag.role-m { color: var(--role-m); }
.role-tag.role-f { color: var(--role-f); }
.role-tag.role-b { color: var(--role-b); }
/* 聲部是每段各自的，同一行可以男女對唱 */
.seg.role-m .tx { color: var(--role-m); }
.seg.role-f .tx { color: var(--role-f); }
.seg.role-b .tx { color: var(--role-b); }

.seg { display: flex; flex-direction: column; }
.seg .ch {
  color: var(--chord); font-weight: 700;
  font-size: .78em; line-height: 1.35;
  white-space: pre; padding-right: .5em;
  font-family: "SF Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
}
/* 每段都保留歌詞列的高度，否則行尾「只有和弦」的那段會掉到歌詞那一行 */
.seg .tx { white-space: pre-wrap; min-height: 1.4em; }
.line.no-lyrics .tx { display: none; }

.sheet.hide-chords .ch { display: none; }
.sheet.hide-lyrics .tx { display: none; }
.sheet.hide-lyrics .seg .ch { padding-right: .8em; }

.sheet-img { margin: 14px 0; }
.sheet-img img {
  max-width: 100%; border: 1px solid var(--border);
  border-radius: var(--radius); background: #fff;
}
.sheet-img figcaption { font-size: .78em; color: var(--muted); margin-top: 4px; }
/* TAB 圖多半是白底，深色模式下調暗一點才不刺眼，但要維持看得清楚 */
:root[data-theme="dark"] .sheet-img img { filter: brightness(.88); }

.chord-summary {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 12px 0; border-top: 1px dashed var(--border); margin-top: 8px;
}
.chord-summary .lbl { font-size: 12px; color: var(--muted); margin-right: 4px; }
.chord-summary .c {
  font-weight: 700; color: var(--chord); font-size: 14px;
  padding: 3px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel);
}

/* ---------- 編輯器 ---------- */
.editor-grid { display: grid; gap: 16px; grid-template-columns: 1fr; margin-top: 16px; }
@media (min-width: 860px) { .editor-grid { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1300px) { .editor-grid { gap: 24px; } .preview { max-height: 72vh; } }
.pane {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
}
.pane h2 { margin: 0 0 12px; font-size: 15px; }

.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--border); }
.tabs .tab {
  border: none; border-bottom: 2px solid transparent; border-radius: 0;
  padding: 8px 14px; font-size: 14px; color: var(--muted); background: transparent;
}
.tabs .tab:hover { color: var(--text); }
.tabs .tab.on {
  color: var(--accent); border-bottom-color: var(--accent);
  font-weight: 600; background: transparent;
}
.meta-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
.meta-grid .full { grid-column: 1 / -1; }
.editor-body { min-height: 320px; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; line-height: 1.7; }
.dir-status { font-size: 12px; color: var(--muted); }
.dir-status.ok { color: var(--accent); }

/* 編輯器很長，回饋必須固定在畫面上，不然按鈕在下面時完全看不到 */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(8px);
  max-width: min(560px, calc(100% - 32px));
  background: var(--text); color: var(--bg);
  padding: 11px 18px; border-radius: 10px; box-shadow: var(--shadow);
  font-size: 14px; line-height: 1.45; z-index: 100;
  opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.warn { background: var(--chord); color: #fff; }

.editor-body.dropping { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--panel)); }
.preview { max-height: 60vh; overflow: auto; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }
.msg { font-size: 13px; color: var(--muted); }
.msg.warn { color: var(--chord); }

.help-dlg { max-width: 720px; }
.help-dlg h3 { margin: 18px 0 6px; font-size: 14px; }
.help-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.help-table th, .help-table td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.help-table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.help-table code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  padding: 1px 5px; border-radius: 4px;
  display: inline-block; word-break: break-all;
}
.help-table td:first-child { white-space: nowrap; }

/* 窄螢幕（含手機）三欄會擠成一個字一行，改成一列一塊由上往下排 */
@media (max-width: 700px) {
  .help-table thead { display: none; }
  .help-table, .help-table tbody, .help-table tr, .help-table td { display: block; width: 100%; }
  .help-table tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  .help-table td { border: none; padding: 1px 0; }
  .help-table td:first-child { white-space: normal; margin-bottom: 3px; }
  .help-table td:last-child:not(:empty) { margin-top: 5px; }
}

dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); color: var(--text);
  max-width: 700px; width: calc(100% - 32px); padding: 18px;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

/* ---------- 手機 ---------- */
@media (max-width: 700px) {
  .more-btn { display: inline-block; }
  .toolbar { gap: 8px; }
  .toolbar .tool-extra { display: none; }
  .toolbar.open .tool-extra { display: flex; flex-wrap: wrap; gap: 8px 12px; width: 100%; }
  .song-head h1 { font-size: 21px; }
  .editor-body { min-height: 240px; }
  .meta-grid { grid-template-columns: 1fr; }
}

/* ---------- 列印 ---------- */
@media print {
  .topbar, .toolbar, .chord-summary { display: none !important; }
  body { background: #fff; color: #000; }
  .wrap { max-width: none; padding: 0; }
  .sheet { font-size: 15px; }
  .line { page-break-inside: avoid; }
}
