*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --ink: #463e34;
  --ink-soft: rgba(70, 62, 52, 0.62);
  --card: rgba(248, 244, 235, 0.9);
  --line: rgba(70, 62, 52, 0.16);
  --accent: #b5524f;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --tk-h: 0px;
}

html, body { height: 100%; }
body {
  background: #f4efe3;
  overflow: hidden;
  height: 100dvh;
  font-family: var(--sans);
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
}

#c {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ── Repos : vignette et titre ────────────────────────────────────────── */
#vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at 50% 45%, rgba(60, 50, 40, 0) 55%, rgba(60, 50, 40, 0.22) 100%);
  opacity: 0;
  transition: opacity 2.4s ease;
}
body.rest #vignette { opacity: 1; }

#caption {
  position: fixed;
  left: 50%;
  top: max(1.4rem, env(safe-area-inset-top));
  transform: translate(-50%, -0.4rem);
  text-align: center;
  font-family: var(--serif);
  color: var(--ink);
  pointer-events: none;
  opacity: 0;
  transition: opacity 1.6s ease, transform 1.6s ease;
  max-width: calc(100vw - 2rem);
}
body.rest #caption { opacity: 1; transform: translate(-50%, 0); }
#cap-title { font-size: 1.15rem; font-style: italic; letter-spacing: 0.02em; }
#cap-sub { margin-top: 0.2rem; font-size: 0.82rem; color: var(--ink-soft); letter-spacing: 0.04em; }
body.has-tk.rest #caption { top: max(0.9rem, env(safe-area-inset-top)); }

/* ── Barre d'outils ───────────────────────────────────────────────────── */
#toolbar {
  position: fixed;
  top: max(0.8rem, env(safe-area-inset-top));
  right: max(0.8rem, env(safe-area-inset-right));
  display: flex;
  gap: 0.35rem;
  padding: 0.3rem;
  background: rgba(248, 244, 235, 0.55);
  border: 1px solid var(--line);
  border-radius: 999px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 50;
  transition: opacity 0.8s ease;
}
body.playing #toolbar, body.playing #chip, body.playing #keyhint { opacity: 0; pointer-events: none; }
body.playing #toolbar { transition-duration: 1.4s; }
.tb {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.tb svg { width: 20px; height: 20px; }
.tb:hover, .tb:focus-visible { background: rgba(70, 62, 52, 0.09); outline: none; }
.tb.busy { opacity: 0.4; pointer-events: none; }
#btn-sound .i-off { display: none; }
#btn-sound.muted .i-on { display: none; }
#btn-sound.muted .i-off { display: block; }
#btn-sound.muted { color: var(--ink-soft); }
#btn-sound.nomidi { color: var(--ink-soft); }
#btn-sound.nomidi::after { content: 'MIDI'; position: absolute; font-size: 7px; letter-spacing: 0.04em; margin-top: 26px; text-decoration: line-through; }
#btn-sound { position: relative; }

/* Rechargé depuis le site : caché tant que l'écran de départ est là */
body:not(.started) #toolbar, body:not(.started) #keyhint, body:not(.started) #touchkeys { opacity: 0; pointer-events: none; }

/* ── Lecture en cours ─────────────────────────────────────────────────── */
#chip {
  position: fixed;
  top: max(0.8rem, env(safe-area-inset-top));
  left: max(0.8rem, env(safe-area-inset-left));
  display: none;
  align-items: center;
  gap: 0.5rem;
  max-width: calc(100vw - 15rem);
  padding: 0.3rem 0.35rem 0.3rem 0.85rem;
  background: rgba(248, 244, 235, 0.7);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.76rem;
  z-index: 50;
  transition: opacity 0.8s;
}
body.has-player #chip { display: flex; }
#chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chip-stop {
  flex-shrink: 0;
  width: 26px; height: 26px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

/* ── Aide clavier et vélocité ─────────────────────────────────────────── */
#keyhint {
  position: fixed;
  left: max(0.9rem, env(safe-area-inset-left));
  bottom: max(0.8rem, env(safe-area-inset-bottom));
  max-width: calc(100vw - 2rem);
  font-size: 0.72rem;
  line-height: 1.5;
  color: var(--ink-soft);
  pointer-events: none;
  transition: opacity 0.8s;
}
#keyhint kbd {
  font-family: var(--sans);
  font-size: 0.68rem;
  padding: 0 0.3rem;
  border: 1px solid var(--line);
  border-radius: 0.25rem;
  background: rgba(248, 244, 235, 0.6);
}
body.has-tk #keyhint { display: none; }

#vel {
  position: fixed;
  right: max(0.7rem, env(safe-area-inset-right));
  top: 8%;
  bottom: 8%;
  width: 3px;
  border-radius: 2px;
  background: linear-gradient(rgba(70, 62, 52, 0.08), rgba(70, 62, 52, 0.3));
  opacity: 0;
  transition: opacity 0.5s;
  pointer-events: none;
}
#vel.show { opacity: 1; transition-duration: 0.1s; }
#vel span {
  position: absolute;
  left: 50%;
  width: 11px; height: 11px;
  margin: -5.5px 0 0 -5.5px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.7;
}

/* ── Clavier tactile ──────────────────────────────────────────────────── */
#touchkeys {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(clamp(84px, 16vh, 128px) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: none;
  background: rgba(70, 62, 52, 0.12);
  border-top: 1px solid var(--line);
  z-index: 40;
  transition: opacity 0.6s;
}
body.has-tk #touchkeys { display: flex; }
.tk-down, .tk-up {
  flex: 0 0 30px;
  border: 0;
  background: rgba(248, 244, 235, 0.55);
  color: var(--ink);
  font-size: 1.3rem;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.tk-board { position: relative; flex: 1; touch-action: none; overflow: hidden; }
.tk-key {
  position: absolute;
  top: 0;
  background: linear-gradient(rgba(250, 247, 240, 0.86), rgba(240, 233, 220, 0.94));
  border-right: 1px solid rgba(70, 62, 52, 0.2);
  border-radius: 0 0 4px 4px;
  pointer-events: none;
}
.tk-key[data-label]::after {
  content: attr(data-label);
  position: absolute;
  left: 0; right: 0; bottom: 6px;
  text-align: center;
  font-size: 0.6rem;
  color: var(--ink-soft);
}
.tk-key.black {
  background: linear-gradient(#5a5046, #3f372f);
  border: 0;
  z-index: 2;
}
.tk-key.down { background: linear-gradient(rgba(230, 200, 180, 0.95), rgba(220, 182, 160, 0.95)); }
.tk-key.black.down { background: linear-gradient(#8a5a50, #6a443c); }

/* ── Écran de départ ──────────────────────────────────────────────────── */
#start {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(244, 239, 227, 0.62);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 600;
  transition: opacity 0.6s;
}
body.started #start { opacity: 0; pointer-events: none; }
#start-panel {
  width: min(30rem, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 1.8rem 1.6rem 1.4rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 1rem;
  box-shadow: 0 12px 40px rgba(70, 62, 52, 0.14);
  text-align: center;
}
#start-panel h1 {
  font-family: var(--serif);
  font-weight: normal;
  font-style: italic;
  font-size: 2rem;
  margin-bottom: 0.7rem;
}
#start-panel p { font-size: 0.9rem; line-height: 1.55; }
#midi-status {
  margin-top: 0.9rem;
  font-size: 0.8rem !important;
  color: var(--ink-soft);
}
#midi-status.ok { color: #4f7a3a; }
#start-buttons { display: flex; gap: 0.7rem; justify-content: center; flex-wrap: wrap; margin: 1.2rem 0 0.9rem; }
.start-btn {
  min-width: 9.5rem;
  padding: 0.75rem 1.3rem;
  border: 1px solid var(--ink);
  border-radius: 999px;
  background: var(--ink);
  color: #f8f4eb;
  font-family: var(--serif);
  font-size: 1.02rem;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s;
  touch-action: manipulation;
}
.start-btn:hover, .start-btn:focus-visible { transform: translateY(-1px); outline: none; background: #2f2922; }
.start-btn.ghost { background: transparent; color: var(--ink); }
.start-btn.ghost:hover, .start-btn.ghost:focus-visible { background: rgba(70, 62, 52, 0.08); }
.start-sub { font-size: 0.76rem !important; color: var(--ink-soft); margin-top: 0.35rem; }
.start-sub kbd { font-family: var(--sans); font-size: 0.72rem; padding: 0 0.28rem; border: 1px solid var(--line); border-radius: 0.25rem; }
#start-keys:empty { display: none; }
.link-btn {
  margin-top: 0.6rem;
  border: 0;
  background: none;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 0.86rem;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: rgba(70, 62, 52, 0.35);
  cursor: pointer;
}
.link-btn:hover, .link-btn:focus-visible { text-decoration-color: var(--ink); outline: none; }
.link-btn[hidden] { display: none; }

/* ── Confirmation ─────────────────────────────────────────────────────── */
#confirm {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(60, 50, 40, 0.2);
  z-index: 700;
}
#confirm.show { display: flex; }
#confirm-card {
  width: min(24rem, 100%);
  padding: 1.3rem 1.3rem 1.1rem;
  background: #f8f4eb;
  border: 1px solid var(--line);
  border-radius: 0.9rem;
  box-shadow: 0 12px 40px rgba(70, 62, 52, 0.2);
  text-align: center;
}
#confirm-msg { font-size: 0.9rem; line-height: 1.5; }
#confirm-buttons { display: flex; gap: 0.6rem; justify-content: center; margin-top: 1rem; flex-wrap: wrap; }
#confirm-buttons button {
  padding: 0.55rem 1.1rem;
  border: 1px solid var(--ink);
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  font-size: 0.85rem;
  cursor: pointer;
}
#confirm-ok { background: var(--accent) !important; border-color: var(--accent) !important; color: #fff !important; }

/* ── Toast, dépôt, debug ──────────────────────────────────────────────── */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(max(1.4rem, env(safe-area-inset-bottom)) + var(--tk-h));
  transform: translate(-50%, 0.8rem);
  max-width: min(32rem, calc(100vw - 2rem));
  padding: 0.55rem 1rem;
  background: rgba(70, 62, 52, 0.9);
  color: #f8f4eb;
  border-radius: 0.6rem;
  font-size: 0.8rem;
  line-height: 1.4;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s, transform 0.3s;
  z-index: 800;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#drop-hint {
  position: fixed;
  inset: 1rem;
  display: none;
  align-items: center;
  justify-content: center;
  border: 2px dashed rgba(70, 62, 52, 0.45);
  border-radius: 1rem;
  background: rgba(244, 239, 227, 0.6);
  font-family: var(--serif);
  font-size: 1.15rem;
  text-align: center;
  padding: 1rem;
  z-index: 900;
  pointer-events: none;
}
body.dragging #drop-hint { display: flex; }

#debug {
  position: fixed;
  left: 0.6rem;
  bottom: calc(2.6rem + var(--tk-h));
  padding: 0.5rem 0.7rem;
  background: rgba(30, 26, 22, 0.78);
  color: #e8e1d2;
  font: 11px/1.45 ui-monospace, Menlo, monospace;
  border-radius: 0.4rem;
  pointer-events: none;
  z-index: 950;
  white-space: pre;
}

/* Écran étroit : le titre du repos passe sous la barre d'outils */
@media (max-width: 640px) {
  #caption, body.has-tk.rest #caption { top: calc(max(0.8rem, env(safe-area-inset-top)) + 3.2rem); }
}
@media (max-width: 520px) {
  .tb { width: 34px; height: 34px; }
  #toolbar { gap: 0.15rem; }
  #chip { max-width: calc(100vw - 14.5rem); }
  #start-panel { padding: 1.4rem 1.1rem 1.1rem; }
  #start-panel h1 { font-size: 1.7rem; }
}
@media (max-height: 480px) {
  #start-panel { padding: 1rem 1.2rem; }
  #start-panel h1 { font-size: 1.5rem; margin-bottom: 0.3rem; }
  #start-buttons { margin: 0.7rem 0 0.4rem; }
}

/* ── Instruments ──────────────────────────────────────────────────────── */
.pop {
  position: fixed;
  top: calc(max(0.8rem, env(safe-area-inset-top)) + 3.1rem);
  right: max(0.8rem, env(safe-area-inset-right));
  display: none;
  flex-direction: column;
  min-width: 11rem;
  padding: 0.35rem;
  background: rgba(248, 244, 235, 0.95);
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  box-shadow: 0 10px 30px rgba(70, 62, 52, 0.16);
  z-index: 60;
}
.pop.show { display: flex; }
.pop button, .inst-seg button {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 40px;
  padding: 0.35rem 0.7rem;
  border: 0;
  border-radius: 0.55rem;
  background: transparent;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 0.95rem;
  cursor: pointer;
  text-align: left;
}
.pop button svg, .inst-seg button svg { width: 20px; height: 20px; flex-shrink: 0; }
.pop button:hover, .pop button:focus-visible, .inst-seg button:hover, .inst-seg button:focus-visible { background: rgba(70, 62, 52, 0.08); outline: none; }
.pop button.on, .inst-seg button.on { background: rgba(70, 62, 52, 0.12); }
.inst-seg { display: flex; flex-wrap: wrap; gap: 0.3rem; justify-content: center; margin-top: 1rem; }
.inst-seg button { flex-direction: column; gap: 0.2rem; padding: 0.45rem 0.6rem; font-size: 0.78rem; min-width: 4.6rem; text-align: center; }

/* ── Réglages ─────────────────────────────────────────────────────────── */
.pop-set { min-width: 17rem; max-width: min(21rem, calc(100vw - 1.6rem)); padding: 0.6rem 0.8rem; gap: 0.2rem; max-height: calc(100dvh - 6rem); overflow-y: auto; }
.set-sec { padding: 0.45rem 0; border-top: 1px solid var(--line); }
.set-sec:first-child { border-top: 0; }
.set-sec h3 { font-family: var(--serif); font-weight: normal; font-style: italic; font-size: 0.92rem; margin-bottom: 0.35rem; }
.set-chk { display: flex; gap: 0.55rem; align-items: flex-start; font-size: 0.85rem; cursor: pointer; }
.set-chk input { margin-top: 0.2rem; accent-color: var(--accent); width: 1rem; height: 1rem; }
.set-chk small { display: block; color: var(--ink-soft); font-size: 0.72rem; line-height: 1.35; margin-top: 0.1rem; }
.set-row { display: flex; justify-content: space-between; align-items: center; gap: 0.6rem; margin-top: 0.45rem; font-size: 0.85rem; }
.set-range { display: flex; align-items: center; gap: 0.5rem; }
.set-range input { width: 8.5rem; accent-color: var(--accent); }
.set-range input:disabled { opacity: 0.45; }
.set-range output { min-width: 4.2em; text-align: right; font-size: 0.82rem; font-variant-numeric: tabular-nums; color: var(--ink); }
.set-row select { font: inherit; font-size: 0.82rem; padding: 0.25rem 0.4rem; border: 1px solid var(--line); border-radius: 0.4rem; background: rgba(255, 255, 255, 0.6); color: var(--ink); }
.seg { display: flex; flex-wrap: wrap; gap: 0.25rem; }
.seg button { flex: 1 1 auto; min-height: 34px; padding: 0.3rem 0.5rem; border: 1px solid var(--line); border-radius: 0.45rem; background: transparent; color: var(--ink); font-size: 0.78rem; cursor: pointer; }
.seg button.on { background: var(--ink); color: #f8f4eb; border-color: var(--ink); }
.h-season { margin-top: 0.6rem; }
/* nuit : textes posés sur le ciel en clair */
body.dark #caption, body.dark #keyhint { color: #eef0f8; }
body.dark #cap-sub { color: rgba(238, 240, 248, 0.7); }
body.dark #keyhint kbd { background: rgba(30, 36, 60, 0.4); border-color: rgba(238, 240, 248, 0.3); }
body.dark #vignette { background: radial-gradient(ellipse at 50% 45%, rgba(10, 14, 30, 0) 55%, rgba(10, 14, 30, 0.35) 100%); }

/* ── Mobile ───────────────────────────────────────────────────────────── */
body { touch-action: manipulation; }
.phone-only { display: none; }
.inst-grid, .act-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.35rem; }
.inst-grid button { display: flex; align-items: center; gap: 0.5rem; min-height: 44px; padding: 0.3rem 0.6rem; border: 1px solid var(--line); border-radius: 0.55rem; background: transparent; color: var(--ink); font-family: var(--serif); font-size: 0.9rem; }
.inst-grid button svg { width: 20px; height: 20px; }
.inst-grid button.on { background: var(--ink); color: #f8f4eb; }
.act-grid button { min-height: 44px; padding: 0.4rem 0.5rem; border: 1px solid var(--line); border-radius: 0.55rem; background: rgba(255, 255, 255, 0.5); color: var(--ink); font-size: 0.82rem; }
.act-grid button[hidden] { display: none; }
@media (max-width: 640px) {
  .phone-only { display: block; }
  #btn-inst, #btn-new, #btn-png, #btn-mid, #btn-fs { display: none; }
  .tb { width: 44px; height: 44px; }
  .seg button { min-height: 40px; }
  .pop-set { left: max(0.8rem, env(safe-area-inset-left)); right: max(0.8rem, env(safe-area-inset-right)); max-width: none; min-width: 0; }
}
/* touches plus hautes en portrait, deux rangées si demandé */
body.has-tk.portrait #touchkeys { height: calc(clamp(110px, 21vh, 170px) + env(safe-area-inset-bottom)); }
body.has-tk.tk2 #touchkeys { height: calc(clamp(170px, 32vh, 300px) + env(safe-area-inset-bottom)); }

/* mode immersif : seulement le jardin et le clavier, une poignée pour revenir */
#btn-immersive {
  position: fixed;
  top: max(0.6rem, env(safe-area-inset-top));
  right: max(0.6rem, env(safe-area-inset-right));
  width: 44px; height: 44px;
  display: none;
  align-items: center; justify-content: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(248, 244, 235, 0.45);
  color: var(--ink);
  opacity: 0.55;
  z-index: 70;
}
#btn-immersive svg { width: 20px; height: 20px; }
body.immersive #btn-immersive { display: flex; }
body.immersive #toolbar, body.immersive #caption, body.immersive #keyhint, body.immersive #chip, body.immersive #vel, body.immersive .pop { display: none !important; }
.inst-seg { flex-wrap: nowrap; }
.inst-seg button { flex: 1 1 0; min-width: 0; padding: 0.45rem 0.2rem; }
