:root {
  --bg: #0b0d12;
  --stage: #06070a;
  --surface: #141821;
  --surface-2: #1b2030;
  --border: #262c3a;
  --text: #eef1f7;
  --muted: #8b93a7;
  --accent: #7c9cff;
  --accent-2: #b28cff;
  --accent-ink: #0b0d12;
  --danger: #ff6b6b;
  --radius: 14px;
  --bar-bg: rgba(11, 13, 18, 0.7);
  --top-h: 56px;    /* mis à jour par le JS */
  --bottom-h: 70px; /* mis à jour par le JS */
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  --safe-left: env(safe-area-inset-left);
  --safe-right: env(safe-area-inset-right);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

button, label, a, input { font: inherit; color: inherit; touch-action: manipulation; }
button { -webkit-appearance: none; appearance: none; background: none; border: 0; padding: 0; cursor: pointer; }

/* ---------- Structure ---------- */
.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-top) + 6px) calc(var(--safe-right) + 10px) 6px calc(var(--safe-left) + 10px);
  min-height: calc(var(--safe-top) + 56px);
  background: var(--bar-bg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  z-index: 5;
}

.topbar-left, .topbar-right { display: flex; gap: 4px; flex: 1 0 92px; }
.topbar-right { justify-content: flex-end; }

.title {
  margin: 0;
  min-width: 0;
  font-size: 16px;
  font-weight: 650;
  line-height: 1.15;
  letter-spacing: -0.01em;
  text-align: center;
  text-wrap: balance; /* nom long : deux lignes équilibrées */
}
.title .nowrap { white-space: nowrap; }
@media (max-width: 359px) { .title { font-size: 12px; } }

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: var(--text);
  text-decoration: none;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}
.icon-btn:active { background: var(--surface-2); transform: scale(0.94); }
.icon-btn:disabled { opacity: 0.28; }
.icon-btn:disabled:active { background: none; transform: none; }
.history .icon-btn:not(:disabled) { background: var(--surface); }

/* ---------- Scène ---------- */
/* La photo occupe tout l'écran ; les barres flottent par-dessus. */
.stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--stage);
  touch-action: none;
}

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: calc(var(--top-h) + 24px) 24px calc(var(--bottom-h) + 24px);
  text-align: center;
}

.empty-hint { margin: 0; color: var(--muted); font-size: 14px; max-width: 30ch; line-height: 1.45; }

.canvas-wrap {
  position: absolute;
  transform-origin: 0 0;
  will-change: transform;
}

.canvas-wrap canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
.zoom-reset {
  position: absolute;
  top: calc(var(--top-h) + 10px);
  right: calc(var(--safe-right) + 10px);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: rgba(20, 24, 33, 0.85);
  border: 1px solid var(--border);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--bottom-h) + 14px);
  transform: translate(-50%, 20px);
  padding: 9px 16px;
  border-radius: 999px;
  background: rgba(20, 24, 33, 0.92);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 550;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Barres du bas ---------- */
.bottom-bars {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  background: var(--bar-bg);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.slider { display: flex; align-items: center; gap: 12px; min-height: 40px; }
.slider-label { font-size: 13px; color: var(--muted); width: 58px; flex: none; }
.slider output { width: 26px; text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  min-width: 0;
  height: 40px;
  background: transparent;
  margin: 0;
  touch-action: pan-x;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -11px;
  border-radius: 50%;
  background: #fff;
  border: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type="range"]::-moz-range-thumb { width: 28px; height: 28px; border-radius: 50%; background: #fff; border: 0; box-shadow: 0 2px 8px rgba(0,0,0,.5); }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 650;
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s, background 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn svg { width: 20px; height: 20px; stroke-width: 2.2; }
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: var(--accent-ink); }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-big { min-height: 54px; padding: 0 26px; font-size: 17px; border-radius: 16px; }
.btn-icon-only { width: 40px; padding: 0; }

/* ---------- Barre d'outils ---------- */
.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 6px calc(var(--safe-right) + 8px) calc(var(--safe-bottom) + 6px) calc(var(--safe-left) + 8px);
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 58px;
  border-radius: 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  transition: background 0.15s, color 0.15s, transform 0.1s, opacity 0.15s;
}
.tool svg { width: 26px; height: 26px; }
.tool:active { transform: scale(0.94); }
.tool:disabled { opacity: 0.3; }
.tool--save:not(:disabled) { color: var(--text); }
.tool--save:not(:disabled) svg { color: #7ee2a8; }

/* ---------- Feuille d'export ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  animation: fade 0.2s ease;
}

.sheet {
  width: 100%;
  max-width: 520px;
  max-height: calc(100% - var(--safe-top) - 20px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 8px calc(var(--safe-right) + 18px) calc(var(--safe-bottom) + 18px) calc(var(--safe-left) + 18px);
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  animation: slide-up 0.25s cubic-bezier(.2,.8,.2,1);
}

.sheet-grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 4px; }
.sheet h2 { margin: 0; font-size: 19px; text-align: center; }

.sheet-preview {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  justify-content: center;
}
.sheet-preview img {
  max-width: 100%;
  max-height: 42vh;
  max-height: 42dvh;
  border-radius: 12px;
  object-fit: contain;
  background: var(--stage);
  -webkit-touch-callout: default; /* appui long → « Ajouter à Photos » */
}

.sheet-hint { margin: 0; text-align: center; font-size: 13px; color: var(--muted); line-height: 1.4; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; }
.sheet-actions .btn { width: 100%; }

@keyframes fade { from { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(100%); } }

/* ---------- Enigme-moi sur le visage ---------- */
.empty-art {
  width: 96px;
  height: 96px;
  border-radius: 28px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #1e2433, #11141c);
  border: 1px solid var(--border);
  color: #c3a6ff;
}
.empty-art svg { width: 44px; height: 44px; stroke-width: 1.5; }
#original { opacity: 0; pointer-events: none; transition: opacity 0.12s; }
.canvas-wrap.show-original #original { opacity: 1; }

.status {
  position: absolute;
  top: calc(var(--top-h) + 10px);
  left: 50%;
  transform: translateX(-50%);
  width: 180px;
  padding: 8px 14px 10px;
  border-radius: 14px;
  background: rgba(20, 24, 33, 0.85);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  pointer-events: none;
  z-index: 2;
}
.progress { display: block; height: 4px; margin-top: 7px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.progress-bar { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #c3a6ff, #6f93b8); transition: width 0.2s; }

.controls { padding: 10px calc(var(--safe-right) + 14px) 0 calc(var(--safe-left) + 14px); }
.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.3; text-align: center; }
.slider .end { width: auto; color: var(--text); font-size: 13px; }
.slider { max-width: 560px; margin: 0 auto; }
#btn-original { -webkit-user-select: none; user-select: none; touch-action: none; }

@media (max-height: 640px) { .tool { min-height: 50px; } }
.toolbar { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 700px) { .toolbar { grid-template-columns: repeat(2, 140px); justify-content: center; } }

/* ---------- Encoder / Décoder + code ---------- */
.controls > * { max-width: 560px; margin-left: auto; margin-right: auto; }
.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.mode {
  min-height: 38px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
}
.mode.is-active { background: var(--surface-2); color: var(--text); box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06) inset; }
.code-row { display: flex; gap: 8px; margin-top: 8px; }
.code-field { position: relative; flex: 1; min-width: 0; display: flex; }
#code {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 44px 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px; /* 16 px : iOS ne zoome pas sur le champ */
  -webkit-user-select: text;
  user-select: text;
  outline: none;
}
#code:focus { border-color: var(--accent); }
#code::placeholder { color: var(--muted); }
#code.is-masked { -webkit-text-security: disc; }
.code-eye {
  position: absolute;
  right: 2px; top: 2px; bottom: 2px;
  width: 40px;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.code-eye[aria-pressed="true"] { color: var(--accent); }
.code-ok { min-height: 44px; min-width: 64px; border-radius: 12px; font-weight: 700; }
.code-ok:disabled { opacity: 0.45; }
.controls .hint { margin-top: 6px; margin-bottom: 2px; }
