:root {
  --bg: #eef0f3;
  --panel: #ffffff;
  --panel-2: #f5f6f8;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #d6dae1;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --accent-soft: #dbe7ff;
  --danger: #b91c1c;
  --paper: #ffffff;
  --key-white: #ffffff;
  --key-black: #1f2328;
  --key-active: #93b4f5;
  --bar-h: 56px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b;
    --panel: #1f2227;
    --panel-2: #272b31;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #363b43;
    --accent: #5b8def;
    --accent-soft: #263a63;
    --danger: #f87171;
    --key-white: #f3f4f6;
    --key-black: #0b0c0e;
  }
}
:root[data-theme="dark"] {
  --bg: #15171b; --panel: #1f2227; --panel-2: #272b31; --text: #e5e7eb; --muted: #9ca3af;
  --border: #363b43; --accent: #5b8def; --accent-soft: #263a63; --danger: #f87171;
  --key-white: #f3f4f6; --key-black: #0b0c0e;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
input, select { -webkit-user-select: text; user-select: text; }

.view { position: fixed; inset: 0; display: flex; flex-direction: column; }
.view[hidden] { display: none; }

.bar {
  display: flex; align-items: center; gap: 8px;
  min-height: var(--bar-h);
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.bar h1 { font-size: 20px; margin: 0; }
.spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel-2);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); }
.icon-btn {
  width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
  border: none; border-radius: 10px; background: transparent;
}
.icon-btn:active, .btn:active, .tool:active { filter: brightness(0.92); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn:disabled { opacity: 0.35; }
.icon-btn.play { background: var(--accent); color: var(--accent-ink); }

/* Bibliotheque */
.lib-body { flex: 1; overflow: auto; padding: 16px max(16px, env(safe-area-inset-left)); }
.score-list { list-style: none; margin: 0 auto; padding: 0; max-width: 900px; display: grid; gap: 10px; }
.score-list li {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px;
}
.score-list .open { flex: 1; text-align: left; background: none; border: none; padding: 4px 0; min-height: 44px; }
.score-list .open strong { display: block; font-size: 17px; }
.score-list .open span { color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); margin-top: 40px; }
.lib-foot { padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 13px; text-align: center; }
.lib-foot a { color: inherit; }

/* Editeur */
.title-input {
  min-width: 120px; max-width: 280px; flex: 0 1 280px;
  border: 1px solid transparent; border-radius: 8px; padding: 6px 8px;
  background: transparent; font-weight: 600; font-size: 17px;
}
.title-input:focus { border-color: var(--border); background: var(--panel-2); outline: none; }
.save-status { color: var(--muted); font-size: 12px; white-space: nowrap; }
.save-status.err { color: var(--danger); }
.tempo { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.tempo input { width: 58px; min-height: 36px; border: 1px solid var(--border); border-radius: 8px; padding: 0 6px; background: var(--panel-2); }

.palette {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 6px max(12px, env(safe-area-inset-left));
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.palette .group { display: flex; gap: 3px; padding-right: 6px; border-right: 1px solid var(--border); }
.palette .group:last-child { border-right: none; }
.menu { position: relative; }
.menu-list {
  display: none; position: absolute; top: calc(100% + 4px); left: 0; z-index: 30; min-width: 250px;
  flex-direction: column; padding: 4px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
}
.menu-list.menu-right { left: auto; right: 0; }
.menu.open .menu-list { display: flex; }
.menu.open .menu-btn { background: var(--accent-soft); border-color: var(--accent); }
.menu-item {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  min-height: 36px; padding: 0 10px; border: none; border-radius: 6px; background: none; text-align: left; white-space: nowrap;
}
.menu-item:hover { background: var(--panel-2); }
.menu-item kbd { font-family: inherit; font-size: 12px; color: var(--muted); }
.menu-item.toggle::before { content: ""; display: inline-block; width: 14px; flex: none; margin-right: -8px; }
.menu-item.toggle.on::before { content: "✓"; color: var(--accent); font-weight: 700; }
.menu-item.toggle.on { background: none; color: inherit; }
.cap-inline { font-size: 13px; color: var(--muted); }
.tool {
  min-width: 36px; height: 40px; padding: 0 7px;
  border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2);
  display: inline-flex; align-items: center; justify-content: center; white-space: nowrap; font-size: 14px;
}
.tool.sym { font-size: 22px; }
#dot { font-size: 22px; }
.tool svg { width: 22px; height: 30px; }
.tool.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tool.danger { color: var(--danger); border-color: var(--danger); font-weight: 600; }
.mix { min-height: 40px; max-width: 220px; border: 1px solid var(--border); border-radius: 10px; padding: 0 8px; background: var(--panel-2); }
.lyric-input {
  position: fixed; z-index: 25; width: 90px; height: 28px; transform: translateX(-50%);
  border: 2px solid var(--accent); border-radius: 6px; background: #fff; color: #111;
  font: 15px Georgia, serif; text-align: center; padding: 0 4px;
}
.selrect {
  position: fixed; z-index: 15; pointer-events: none;
  border: 1.5px solid var(--accent); background: rgba(37, 99, 235, 0.12); border-radius: 3px;
}

.score-wrap { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; padding: 16px; }
.sheet {
  background: var(--paper); color: #111; margin: 0 auto; max-width: 1400px;
  border-radius: 6px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15); padding: 18px 8px 24px;
}
.sheet-title { text-align: center; font: 600 24px Georgia, "Times New Roman", serif; margin: 0 0 2px; }
.sheet-composer { text-align: right; margin: 0 24px 4px; font: italic 14px Georgia, serif; min-height: 1em; }
#score { touch-action: manipulation; }
.ghost { position: fixed; pointer-events: none; z-index: 20; --z: 1; }
.ghost i {
  position: absolute; left: 0; top: 0;
  width: calc(12px * var(--z)); height: calc(9px * var(--z));
  transform: translate(-50%, -50%) rotate(-22deg);
  border-radius: 50%; background: rgba(37, 99, 235, 0.55);
}
.ghost span {
  position: absolute; left: calc(12px * var(--z)); top: -30px;
  background: #1f2937; color: #fff; font-size: 12px; padding: 2px 6px; border-radius: 5px; white-space: nowrap;
}
#score svg { display: block; }
#score .playing path, #score .playing rect { fill: #e11d48 !important; stroke: #e11d48 !important; }

.piano-bar { background: var(--panel); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); }
.piano-head { display: flex; align-items: center; gap: 10px; padding: 6px max(12px, env(safe-area-inset-left)); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg-btn { min-height: 36px; padding: 0 12px; border: none; background: var(--panel-2); }
.seg-btn.on { background: var(--accent); color: var(--accent-ink); }
.sel-info { color: var(--muted); font-size: 13px; }

.piano { position: relative; height: 130px; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; }
.piano.collapsed { display: none; }
.keys-inner { position: relative; height: 100%; }
.wkey, .bkey { position: absolute; top: 0; border: 1px solid #555; border-radius: 0 0 6px 6px; touch-action: manipulation; }
.wkey { height: 100%; width: 44px; background: var(--key-white); z-index: 1; }
.bkey { height: 62%; width: 28px; background: var(--key-black); z-index: 2; }
.wkey.down, .bkey.down { background: var(--key-active); }
.wkey span { position: absolute; bottom: 6px; left: 0; right: 0; text-align: center; font-size: 11px; color: #555; }

/* Dialogues */
.dlg { border: 1px solid var(--border); border-radius: 14px; background: var(--panel); color: var(--text); padding: 20px; width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 40px); }
.staves-ed { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px 10px; margin: 0 0 12px; }
.staves-ed legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.staff-row { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.staff-row input:not([type]) { flex: 1 1 140px; min-width: 110px; }
.staff-row select { flex: 0 1 210px; }
.dlg .staff-row .brace-opt { flex-direction: row; align-items: center; gap: 4px; margin: 0; font-size: 13px; color: var(--text); }
.dlg .staff-row .brace-opt input { min-height: 0; }
.dlg::backdrop { background: rgba(0, 0, 0, 0.4); }
.dlg h3 { margin: 0 0 12px; }
.dlg label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.dlg input, .dlg select { min-height: 40px; border: 1px solid var(--border); border-radius: 8px; padding: 0 10px; background: var(--panel-2); color: var(--text); font-size: 16px; }
.dlg .row { display: flex; gap: 10px; flex-wrap: wrap; }
.dlg .row label { flex: 1; min-width: 120px; }
.dlg menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 8px 0 0; }
.dlg .hint { color: var(--muted); font-size: 13px; }
.dlg.editing .new-only { display: none; }
.export-list { display: grid; gap: 8px; margin-bottom: 12px; }
.export-list .btn { justify-content: space-between; min-height: 48px; }
.export-list small { color: var(--muted); }
.file-like { flex-direction: row !important; color: var(--text) !important; font-size: 14px !important; }
.keys { width: 100%; border-collapse: collapse; font-size: 14px; }
.keys td { padding: 5px 4px; border-bottom: 1px solid var(--border); }
.keys td:first-child { font-weight: 600; white-space: nowrap; width: 110px; }

.toast {
  position: fixed; left: 50%; bottom: calc(160px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50;
  max-width: calc(100vw - 32px); font-size: 14px; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

@media (max-width: 700px) {
  .tempo span { display: none; }
  .title-input { max-width: 160px; }
}

@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; overflow: visible; }
  .view { position: static; }
  #library, .bar, .palette, .piano-bar, .toast, dialog { display: none !important; }
  .score-wrap { overflow: visible; padding: 0; }
  .sheet { box-shadow: none; padding: 0; max-width: none; }
}
