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

html, body { width: 100%; height: 100%; }

body {
  background: radial-gradient(ellipse at 50% 40%, #2a2520 0%, #1b1815 60%, #141210 100%);
  background-color: #1b1815;
  overflow: hidden;
  height: 100dvh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: #ece4d2;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
body.idle.started { cursor: none; }

/* ── La feuille ──────────────────────────────────────────────────────────── */
#sheet {
  position: fixed;
  isolation: isolate;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25), 0 14px 40px rgba(0, 0, 0, 0.45);
  background: #efe6d0;
}
#sheet canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#stain, #ink { mix-blend-mode: multiply; }

/* Repère du pinceau : une ombre ronde, à peine visible */
#nib {
  --r: 4px;
  --ink: 17, 15, 14;
  position: absolute;
  left: 0; top: 0;
  width: 0; height: 0;
  pointer-events: none;
}
#nib::after {
  content: "";
  position: absolute;
  left: calc(-1 * var(--r) - 3px);
  top: calc(-1 * var(--r) - 3px);
  width: calc(2 * var(--r) + 6px);
  height: calc(2 * var(--r) + 6px);
  border-radius: 50%;
  border: 1px solid rgba(var(--ink), 0.28);
  transition: opacity 0.4s;
}
body:not(.started) #nib, body.full #nib { opacity: 0; }
body.waiting #nib::after { animation: breathe 2.4s ease-in-out infinite; }
@keyframes breathe { 50% { opacity: 0.25; } }

/* ── Niveau du micro ─────────────────────────────────────────────────────── */
#meter {
  position: fixed;
  left: max(0.8rem, env(safe-area-inset-left));
  bottom: max(0.8rem, env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
  padding: 0.4rem 0.45rem;
  border-radius: 0.6rem;
  background: rgba(20, 18, 16, 0.55);
  color: rgba(236, 228, 210, 0.55);
  opacity: 0.75;
  transition: opacity 0.5s, transform 0.5s;
  pointer-events: none;
  z-index: 20;
}
body:not(.started) #meter { opacity: 0; }
/* Interface cachée : le niveau du micro disparaît aussi (immersion) */
body.idle #meter { opacity: 0; transform: translateY(0.4rem); }
#meter svg { width: 15px; height: 15px; }
#meter-bar {
  position: relative;
  width: 4px;
  height: 26px;
  border-radius: 2px;
  background: rgba(236, 228, 210, 0.14);
  overflow: hidden;
}
#meter-fill {
  position: absolute;
  inset: 0;
  background: rgba(236, 228, 210, 0.7);
  transform-origin: bottom;
  transform: scaleY(0);
}
#meter.down #meter-fill { background: #d9a189; }
#meter-gate {
  position: absolute;
  left: -1px; right: -1px;
  height: 1px;
  background: rgba(236, 228, 210, 0.85);
}

/* ── Barre d'outils ──────────────────────────────────────────────────────── */
#toolbar {
  position: fixed;
  left: 50%;
  bottom: max(0.8rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.15rem;
  padding: 0.3rem;
  border-radius: 999px;
  background: rgba(24, 21, 18, 0.78);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  z-index: 30;
  transition: opacity 0.5s, transform 0.5s;
}
body:not(.started) #toolbar,
body.idle #toolbar {
  opacity: 0;
  transform: translate(-50%, 0.6rem);
  pointer-events: none;
}
#toolbar > button {
  width: 2.4rem;
  height: 2.4rem;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: rgba(236, 228, 210, 0.82);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
#toolbar > button:hover { background: rgba(236, 228, 210, 0.1); color: #fff; }
#toolbar > button:focus-visible, #size-range:focus-visible, .ink:focus-visible, #help-close:focus-visible, #full-card button:focus-visible, #start-mic:focus-visible { outline: 1px solid rgba(236, 228, 210, 0.7); outline-offset: 2px; }
#toolbar svg { width: 20px; height: 20px; }

/* Bouton du pinceau : pastille à la couleur et à la taille du pinceau */
#b-brush-dot {
  --c: rgb(17, 15, 14);
  --d: 10px;
  width: var(--d);
  height: var(--d);
  border-radius: 50%;
  background: var(--c);
  box-shadow: 0 0 0 2px rgba(236, 228, 210, 0.75);
  transition: width 0.2s, height 0.2s, background 0.2s;
}

/* ── Mode voix / musique ─────────────────────────────────────────────────── */
#b-mode .i-musique, body.mode-musique #b-mode .i-voix { display: none; }
body.mode-musique #b-mode .i-musique { display: contents; }
#b-mode .i-voix { display: contents; }

/* Barre du mode musique, en haut : lecture, nom du morceau, importer, micro */
#music-bar {
  position: fixed;
  left: 50%;
  top: max(0.8rem, env(safe-area-inset-top));
  transform: translateX(-50%);
  display: none;
  align-items: center;
  gap: 0.15rem;
  max-width: calc(100vw - 1.6rem);
  padding: 0.3rem;
  border-radius: 999px;
  background: rgba(24, 21, 18, 0.78);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  color: rgba(236, 228, 210, 0.82);
  font-size: 0.82rem;
  z-index: 30;
  transition: opacity 0.5s, transform 0.5s;
}
body.started.mode-musique #music-bar { display: flex; }
body.idle #music-bar { opacity: 0; transform: translate(-50%, -0.6rem); pointer-events: none; }
#music-bar button {
  width: 2.2rem;
  height: 2.2rem;
  flex: none;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
#music-bar button[hidden] { display: none; }
#music-bar button:hover { background: rgba(236, 228, 210, 0.1); color: #fff; }
#music-bar button:focus-visible, #b-mode:focus-visible { outline: 1px solid rgba(236, 228, 210, 0.7); outline-offset: 2px; }
#music-bar svg { width: 18px; height: 18px; }
#m-play .i-pause, #m-play.playing .i-play { display: none; }
#m-play.playing .i-pause { display: block; }
#m-name {
  padding: 0 0.6rem;
  max-width: 22rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* Écran d'accueil : choix du mode */
#mode-pick {
  display: inline-flex;
  margin: -0.2rem auto 1rem;
  padding: 0.2rem;
  border-radius: 999px;
  background: rgba(29, 26, 23, 0.08);
}
#mode-pick button {
  font: inherit;
  font-size: 0.9rem;
  padding: 0.35rem 1.1rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: #6b6156;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
#mode-pick button[aria-pressed="true"] { background: #1d1a17; color: #f4eee0; }
#mode-pick button:focus-visible, #start-file:focus-visible { outline: 1px solid #1d1a17; outline-offset: 2px; }
#start .for-musique, body.mode-musique #start .for-voix { display: none; }
body.mode-musique #start .for-musique { display: block; }
#start-file {
  margin: -0.5rem auto 1rem;
  font: inherit;
  font-size: 0.88rem;
  border: 0;
  background: none;
  color: #4a4138;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}
body.mode-musique #start #start-file { display: block; }

/* ── Panneau du pinceau ──────────────────────────────────────────────────── */
#brush-panel {
  position: fixed;
  left: 50%;
  bottom: calc(max(0.8rem, env(safe-area-inset-bottom)) + 3.6rem);
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 1.6rem);
  padding: 0.75rem 0.9rem 0.7rem;
  border-radius: 0.8rem;
  background: rgba(24, 21, 18, 0.9);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  z-index: 32;
}
#brush-panel[hidden], body.idle #brush-panel { display: none; }
#inks {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.45rem;
}
.ink {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  border: 2px solid rgba(236, 228, 210, 0.2);
  background: var(--c);
  cursor: pointer;
  transition: border-color 0.2s, transform 0.2s;
}
.ink:hover { transform: scale(1.1); }
.ink.on { border-color: rgba(236, 228, 210, 0.95); }
#ink-name {
  margin: 0.45rem 0 0.55rem;
  text-align: center;
  font-size: 0.8rem;
  color: rgba(236, 228, 210, 0.7);
}
/* Couleurs automatiques : pastille arc-en-ciel des encres, délai − / + */
.ink-auto { background: var(--c); }
.ink-auto.on { box-shadow: 0 0 0 2px rgba(24, 21, 18, 0.9) inset; }
#auto-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  margin-top: -0.2rem;
  font-size: 0.8rem;
  color: rgba(236, 228, 210, 0.7);
}
#auto-row[hidden] { display: none; }
#auto-sec { min-width: 6.4rem; text-align: center; font-variant-numeric: tabular-nums; }
#auto-row button {
  width: 1.7rem;
  height: 1.7rem;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: rgba(236, 228, 210, 0.82);
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, opacity 0.2s;
}
#auto-row button:hover { background: rgba(236, 228, 210, 0.1); color: #fff; }
#auto-row button:disabled { opacity: 0.3; cursor: default; background: transparent; }
#auto-row button:focus-visible { outline: 1px solid rgba(236, 228, 210, 0.7); outline-offset: 2px; }

/* Curseur de taille, directement dans la barre */
#size-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0 0.5rem 0 0.25rem;
  font-size: 0.78rem;
  color: rgba(236, 228, 210, 0.75);
}
#size-range { width: 7.5rem; accent-color: #d8cdb6; cursor: pointer; }
#size-value { width: 3.1rem; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
@media (max-width: 520px) {
  /* La barre prend toute la largeur : le niveau du micro monte en haut à gauche */
  #meter { bottom: auto; top: max(0.8rem, env(safe-area-inset-top)); }
}
@media (max-width: 420px) {
  #size-range { width: 5.5rem; }
  #size-value { width: 2.6rem; }
}

/* ── Aide ────────────────────────────────────────────────────────────────── */
#help {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(20, 18, 15, 0.35);
  z-index: 55;                   /* au-dessus de l'accueil */
}
#help[hidden] { display: none; }
#help-panel {
  max-width: 26rem;
  max-height: calc(100dvh - 2rem);
  overflow: auto;
  padding: 1.2rem 1.4rem 1rem;
  border-radius: 0.6rem;
  background: #1b1815;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}
#help h2 { font-size: 1rem; font-weight: 600; margin-bottom: 0.8rem; }
#help dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.4rem 1rem;
  font-size: 0.85rem;
}
#help dt { white-space: nowrap; color: rgba(236, 228, 210, 0.95); }
#help dd { color: rgba(236, 228, 210, 0.72); }
#help kbd {
  font-family: inherit;
  font-size: 0.75rem;
  padding: 0.05rem 0.35rem;
  border: 1px solid rgba(236, 228, 210, 0.3);
  border-radius: 0.25rem;
}
#help-close {
  display: block;
  margin: 1rem auto 0;
  font: inherit;
  font-size: 0.85rem;
  padding: 0.4rem 1rem;
  border-radius: 999px;
  border: 1px solid rgba(236, 228, 210, 0.3);
  background: transparent;
  color: #ece4d2;
  cursor: pointer;
}
#help-close:hover { background: rgba(236, 228, 210, 0.1); }
@media (hover: none) { #b-help { display: none !important; } }

/* Pause : le repère du pinceau respire */
body.paused #nib::after { animation: breathe 1.2s ease-in-out infinite; }

/* ── Feuille pleine ──────────────────────────────────────────────────────── */
#full-card {
  position: fixed;
  left: 50%;
  bottom: calc(max(0.8rem, env(safe-area-inset-bottom)) + 3.6rem);
  transform: translate(-50%, 0.5rem);
  padding: 0.8rem 1rem;
  border-radius: 0.8rem;
  background: rgba(24, 21, 18, 0.88);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s, transform 0.5s;
  z-index: 31;
}
body.full #full-card { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#full-card p { font-size: 0.92rem; margin-bottom: 0.6rem; }
#full-card div { display: flex; gap: 0.5rem; justify-content: center; }
#full-card button {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  border: 1px solid rgba(236, 228, 210, 0.3);
  background: transparent;
  color: #ece4d2;
  cursor: pointer;
  white-space: nowrap;
}
#full-card button:hover { background: rgba(236, 228, 210, 0.1); }

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  left: 50%;
  top: max(0.9rem, env(safe-area-inset-top));
  transform: translate(-50%, -0.4rem);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  background: rgba(24, 21, 18, 0.85);
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s, transform 0.35s;
  z-index: 40;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ── Accueil ─────────────────────────────────────────────────────────────── */
#start {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(20, 18, 15, 0.12);
  z-index: 50;
  transition: opacity 0.6s;
}
body.started #start { opacity: 0; pointer-events: none; }
#start-panel {
  max-width: 30rem;
  padding: 2rem 2rem 1.6rem;
  border-radius: 0.3rem;
  background: rgba(244, 238, 224, 0.94);
  color: #26211c;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  text-align: center;
}
#start h1 {
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  font-weight: 400;
  font-size: 2rem;
  letter-spacing: 0.02em;
  margin-bottom: 0.9rem;
}
#start p { font-size: 0.98rem; line-height: 1.5; }
#start-mic {
  margin: 1.4rem auto 1.1rem;
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font: inherit;
  font-size: 1rem;
  padding: 0.75rem 1.4rem;
  border: 0;
  border-radius: 999px;
  background: #1d1a17;
  color: #f4eee0;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}
#start-mic:hover { background: #3a332c; }
#start-mic:active { transform: scale(0.98); }
#start-mic svg { width: 18px; height: 18px; }
#start .privacy { font-size: 0.8rem; color: #6b6156; }
#start .keys { font-size: 0.78rem; color: #8a7e70; margin-top: 0.6rem; }
#start kbd {
  font-family: inherit;
  font-size: 0.72rem;
  padding: 0.05rem 0.35rem;
  border: 1px solid #c9bda8;
  border-radius: 0.25rem;
}
#start-error { color: #a3291f; font-size: 0.88rem; margin-top: 0.6rem; }
#start-error:empty { display: none; }
body.touch #start .keys { display: none; }

@media (max-height: 480px) {
  #start-panel { padding: 1.1rem 1.4rem 1rem; }
  #start h1 { font-size: 1.5rem; margin-bottom: 0.5rem; }
  #start p { font-size: 0.88rem; }
  #start-mic { margin: 0.8rem auto 0.6rem; }
}
@media (hover: none) { #start .keys { display: none; } }

/* ── Debug ───────────────────────────────────────────────────────────────── */
#debug {
  position: fixed;
  top: 0.6rem;
  right: 0.6rem;
  padding: 0.5rem 0.7rem;
  background: rgba(0, 0, 0, 0.72);
  color: #c9f5d6;
  font: 11px/1.45 ui-monospace, Menlo, monospace;
  border-radius: 0.4rem;
  z-index: 60;
  pointer-events: none;
  white-space: pre;
}

@media (prefers-reduced-motion: reduce) {
  body.waiting #nib::after, body.paused #nib::after { animation: none; }
}
