/* Layout : instrument plein cadre.
   Portrait : signatures + liste en haut, titre et cercle au centre, barre de tempo en bas.
   Paysage : signatures à gauche, cercle au centre, barre de tempo verticale à droite. */
:root {
  --surface-0: #f4f6f3;
  --surface-1: #e4e9e3;
  --surface-2: #ffffff;
  --text-primary: #0f1a12;
  --text-secondary: #5b6a5e;
  --border-subtle: #cfd8cf;
  --border-strong: #0f1a12;
  --accent: #1c6130;
  --accent-soft: #8dbb9a;
  --accent-ink: #ffffff;
  --accent-glow: rgba(28, 97, 48, .4);
  --danger: #a3261b;
  --font-display: "Big Shoulders Display", "Arial Narrow", Impact, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --surface-0: #0b0f0c;
    --surface-1: #18201a;
    --surface-2: #121813;
    --text-primary: #eef3ee;
    --text-secondary: #8a988c;
    --border-subtle: #263027;
    --border-strong: #eef3ee;
    --accent: #34a65a;
    --accent-soft: #2b6b3f;
    --accent-ink: #04140a;
    --accent-glow: rgba(64, 196, 108, .55);
    --danger: #ff7a6b;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --surface-0: #0b0f0c;
  --surface-1: #18201a;
  --surface-2: #121813;
  --text-primary: #eef3ee;
  --text-secondary: #8a988c;
  --border-subtle: #263027;
  --border-strong: #eef3ee;
  --accent: #34a65a;
  --accent-soft: #2b6b3f;
  --accent-ink: #04140a;
  --accent-glow: rgba(64, 196, 108, .55);
  --danger: #ff7a6b;
  color-scheme: dark;
}

* { box-sizing: border-box; touch-action: manipulation; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 15px;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- écran principal ---------- */

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.top { display: flex; gap: 8px; align-items: stretch; }
.sigs { display: flex; gap: 8px; flex: 1; min-width: 0; }
.chip, .square {
  height: 48px;
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  background: transparent;
  font-weight: 600;
  font-size: 16px;
  letter-spacing: .04em;
  font-variant-numeric: tabular-nums;
  display: grid;
  place-items: center;
}
.chip { flex: 1; min-width: 0; padding: 0 4px; }
.chip[aria-pressed="true"] { background: var(--text-primary); color: var(--surface-0); }
.square { width: 48px; flex: none; }
.chip:active, .square:active { background: var(--surface-1); }

.center {
  --ring: 320px;
  position: relative;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.song {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  max-width: 520px;
}
.nav {
  width: 48px;
  height: 48px;
  flex: none;
  border: 0;
  border-radius: 12px;
  background: var(--surface-1);
  font-size: 34px;
  line-height: 1;
}
.nav:disabled { opacity: .25; }
.title {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 12px;
  border: 0;
  border-radius: 12px;
  background: var(--surface-1);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 26px;
  letter-spacing: .02em;
  text-transform: uppercase;
}
.title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.title-text.muted { color: var(--text-secondary); }
.dirty { color: var(--accent); }

/* Cercle : --ring est posé en JS sur .center (le plus grand carré disponible). */
.ring {
  position: relative;
  width: var(--ring);
  height: var(--ring);
  flex: none;
}
.disc {
  position: absolute;
  inset: 13%;
  border-radius: 50%;
  border: 3px solid var(--border-strong);
  display: grid;
  place-items: center;
}
/* Haut-parleur et lecture collés aux bords du disque, loin du chiffre. */
.mute, .play {
  position: absolute;
  left: 50%;
  width: 60px;
  height: 52px;
  margin-left: -30px;
  border: 0;
  background: none;
  display: grid;
  place-items: center;
}
.mute { top: 15%; transform: translateY(-50%); font-size: max(22px, calc(var(--ring) * .065)); line-height: 1; }
.play { bottom: 15%; transform: translateY(50%); }
.bpm, .bpm-input {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: max(80px, calc(var(--ring) * .39));
  line-height: .82;
  letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
  color: inherit;
}
.bpm {
  border: 0;
  background: none;
  padding: 0;
  touch-action: manipulation;
}
.bpm-input {
  width: 80%;
  max-width: 2.6em;
  padding: 0;
  border: 0;
  border-bottom: 4px solid var(--accent);
  border-radius: 0;
  background: none;
  text-align: center;
  -moz-appearance: textfield;
  -webkit-user-select: text;
  user-select: text;
}
.bpm-input::-webkit-inner-spin-button, .bpm-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.bpm-input:focus-visible { outline: none; }
.play-icon {
  width: 0;
  height: 0;
  margin-left: 5px;
  border-style: solid;
  border-width: 14px 0 14px 23px;
  border-color: transparent transparent transparent currentColor;
}
.play.is-playing .play-icon {
  width: 22px;
  height: 22px;
  margin: 0;
  border: 0;
  background: currentColor;
  border-radius: 3px;
}

/* Points de subdivision autour du cercle. */
.points { position: absolute; inset: 0; }
.pt {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  padding: 0;
  border: 0;
  background: none;
  display: grid;
  place-items: center;
  transform: rotate(var(--a)) translateY(calc(var(--ring) * -.44));
}
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--text-secondary);
  transition: width .12s, height .12s, background-color .12s;
}
.pt.beat .dot { width: 13px; height: 13px; border-color: var(--text-primary); }
.pt[data-lvl="1"] .dot { width: 16px; height: 16px; border-color: var(--accent); background: var(--accent-soft); }
.pt[data-lvl="2"] .dot { width: 22px; height: 22px; border-color: var(--accent); background: var(--accent); }
.pt[data-lvl="down"] .dot {
  width: 18px;
  height: 18px;
  border-radius: 3px;
  border-color: var(--text-primary);
  background: var(--text-primary);
  transform: rotate(45deg);
}

/* ---------- barre de tempo ---------- */

.slider { position: relative; height: 76px; }
.track {
  position: absolute;
  inset: 0;
  border: 2px solid var(--border-strong);
  border-radius: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 22px;
  color: var(--text-secondary);
  font-size: 26px;
  font-weight: 600;
  pointer-events: none;
}
.gap { width: 128px; }
.knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 124px;
  height: 58px;
  margin: -29px 0 0 -62px;
  border: 0;
  border-radius: 29px;
  background: var(--text-primary);
  color: var(--surface-0);
  font-weight: 600;
  font-size: 17px;
  letter-spacing: .22em;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  touch-action: none;
  transition: transform .18s ease-out;
}
.knob > span:first-child { margin-right: -.22em; }
.knob.dragging { transition: none; background: var(--accent); color: var(--accent-ink); }
.tap-dots { display: flex; gap: 5px; }
.tap-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: 1.5px solid currentColor;
  opacity: .6;
}
.tap-dots i.on { background: currentColor; opacity: 1; }

/* ---------- paysage ---------- */

@media (orientation: landscape) {
  .app {
    grid-template-rows: minmax(0, 1fr);
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .top { flex-direction: column; justify-content: center; width: 76px; }
  .sigs { flex-direction: column; flex: none; }
  .chip { flex: none; height: 52px; }
  .square { width: 76px; height: 52px; }
  /* Titre dans l'espace libre à gauche du cercle, pour que le cercle prenne toute la hauteur. */
  .center.side-title .song {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: calc((100% - var(--ring)) / 2 - 8px);
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "title title" "prev next";
    align-content: center;
    gap: 8px;
  }
  .center.side-title .title { grid-area: title; height: auto; min-height: 48px; padding: 10px 12px; font-size: 20px; }
  .center.side-title .title-text { white-space: normal; text-align: center; overflow-wrap: normal; }
  .center.side-title #prev { grid-area: prev; justify-self: end; }
  .center.side-title #next { grid-area: next; justify-self: start; }
  .slider { height: auto; width: 76px; }
  .track { flex-direction: column-reverse; padding: 22px 0; }
  .mark { transform: rotate(-90deg); }
  .gap { width: auto; height: 128px; }
  .knob { width: 62px; height: 120px; margin: -60px 0 0 -31px; border-radius: 31px; }
}

/* ---------- bibliothèque ---------- */

.lib {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: var(--surface-0);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
  -webkit-user-select: text;
  user-select: text;
}
.lib-head {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 2px solid var(--border-strong);
}
.lib-head h1 {
  grid-column: 2;
  margin: 0;
  text-align: center;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 28px;
  text-transform: uppercase;
  letter-spacing: .02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lib-icon {
  width: 48px;
  height: 48px;
  border: 0;
  background: none;
  font-size: 28px;
}
#libBack { grid-column: 1; grid-row: 1; }
#libClose { grid-column: 3; grid-row: 1; font-size: 22px; }
.lib-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 16px 24px;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
}
.lib-foot {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}
.lib-foot .btn { flex: 1; }

.rows { list-style: none; margin: 0; padding: 0; }
.row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 48px;
  align-items: center;
  min-height: 64px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--surface-0);
}
.row.dragging {
  position: relative;
  z-index: 1;
  background: var(--surface-1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.row.is-current .row-name { color: var(--accent); }
.handle {
  width: 44px;
  height: 64px;
  display: grid;
  place-items: center;
  font-size: 24px;
  color: var(--text-secondary);
  touch-action: none;
  cursor: grab;
}
.row-main {
  min-width: 0;
  border: 0;
  background: none;
  text-align: left;
  padding: 10px 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.rows:not(#songRows) .row-main { grid-column: 1 / 3; padding-left: 4px; }
.row-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 22px;
  text-transform: uppercase;
  letter-spacing: .02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-num {
  display: inline-block;
  min-width: 1.6em;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}
.row-section {
  margin-bottom: 2px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--accent);
}
.row-meta { font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.row-more {
  width: 48px;
  height: 48px;
  border: 0;
  background: none;
  font-size: 22px;
}
.save-current {
  width: 100%;
  margin-bottom: 12px;
  padding: 14px 16px;
  border: 2px dashed var(--border-strong);
  border-radius: 14px;
  background: none;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.save-current strong { font-size: 16px; }
.save-current span { font-size: 13px; color: var(--text-secondary); word-break: break-all; }
.empty { color: var(--text-secondary); line-height: 1.5; max-width: 40ch; }
.version { margin-top: 32px; font-size: 12px; color: var(--text-secondary); letter-spacing: .08em; }

.btn {
  min-height: 52px;
  padding: 0 16px;
  border: 2px solid var(--border-strong);
  border-radius: 14px;
  background: transparent;
  font-weight: 600;
}
.btn.primary { background: var(--text-primary); color: var(--surface-0); }
.btn.danger { border-color: var(--danger); color: var(--danger); }
.btn:active { opacity: .8; }

/* ---------- feuilles ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-panel {
  width: 100%;
  max-width: 520px;
  max-height: 90%;
  overflow-y: auto;
  background: var(--surface-2);
  border-radius: 20px 20px 0 0;
  padding: 20px 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.sheet-panel h2 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 26px;
  text-transform: uppercase;
  letter-spacing: .02em;
  text-wrap: balance;
}
.sheet-text { margin: 0; color: var(--text-secondary); line-height: 1.5; }
.sheet-actions { display: flex; flex-direction: column; gap: 10px; }
.sheet-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-secondary); }
.field input {
  height: 52px;
  padding: 0 14px;
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 600 18px var(--font-mono);
}
.json {
  width: 100%;
  padding: 12px;
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 500 16px/1.4 var(--font-mono);
  resize: vertical;
  -webkit-user-select: text;
  user-select: text;
}
.form-error { margin: 0; min-height: 1em; color: var(--danger); font-size: 14px; line-height: 1.4; }
.form-error:empty { display: none; }

/* ---------- messages ---------- */

.toast, .update {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: var(--text-primary);
  color: var(--surface-0);
  font-size: 14px;
  font-weight: 600;
  text-align: center;
}
.toast { bottom: max(24px, env(safe-area-inset-bottom)); pointer-events: none; }
.update { top: max(12px, env(safe-area-inset-top)); border: 0; background: var(--accent); color: var(--accent-ink); }
