: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-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: var(--accent);
}
.empty-art svg { width: 44px; height: 44px; stroke-width: 1.5; }

.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;
}
#overlay { pointer-events: none; }

.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);
}

/* ---------- Panneau d'options ---------- */
.panel {
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  padding: 0 calc(var(--safe-right) + 14px) 0 calc(var(--safe-left) + 14px);
}
.panel:not(.is-open) { display: none; }

.panel-body { display: flex; flex-direction: column; gap: 10px; padding: 12px 0 10px; }

.panel-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.panel-head { justify-content: space-between; min-height: 40px; }
.panel-row--sliders { gap: 16px; }
.panel-row--wrap { flex-wrap: wrap; row-gap: 10px; }
.panel-row--wrap .swatches { flex: 1 0 auto; overflow: visible; }
.item-actions { margin-left: auto; }

.panel-group { display: flex; flex-direction: column; gap: 10px; }

.segmented .seg-icon { width: 16px; height: 16px; margin-right: 4px; vertical-align: -3px; stroke-width: 2; }
.segmented button { display: inline-flex; align-items: center; }

/* Baguette magique */
.panel-head--wrap { flex-wrap: wrap; row-gap: 6px; }
.panel-head--wrap .hint { flex: 1 0 150px; }

.wand-info { min-height: 40px; gap: 8px; }
.wand-label { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.segmented button:disabled { opacity: 0.35; }
.wand-src-hint { min-height: 34px; display: flex; align-items: center; }
.wand-src-hint.is-warning { color: #ffb86b; }
.slider output.out-wide { width: 40px; }
.panel-row--sliders .slider { flex: 1; min-width: 0; }

.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.3; flex: 1; min-width: 0; }

.segmented {
  display: inline-flex;
  flex: none;
  padding: 3px;
  border-radius: 11px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.segmented button {
  min-height: 34px;
  padding: 0 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.segmented button[aria-checked="true"] { background: var(--surface-2); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.4); }
.segmented--small button { padding: 0 10px; font-size: 13px; }

.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); }

.item-actions { display: flex; gap: 8px; flex: none; }

/* Émojis */
.emoji-grid {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(2, 48px);
  grid-auto-columns: 48px;
  gap: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  touch-action: pan-x;
  margin: 0 -14px;
  padding: 0 14px;
  scroll-padding: 0 14px;
}
.emoji-grid::-webkit-scrollbar { display: none; }
.emoji-grid button {
  font-size: 30px;
  line-height: 1;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  transition: background 0.12s, transform 0.1s;
}
.emoji-grid button:active { transform: scale(0.9); }
.emoji-grid button[aria-selected="true"] { background: var(--surface-2); box-shadow: inset 0 0 0 2px var(--accent); }

/* Tampon */
.text-input {
  flex: 1;
  min-width: 0;
  height: 42px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 16px; /* évite le zoom automatique d'iOS */
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
.text-input:focus { border-color: var(--accent); }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
  margin: 0 -14px;
  padding: 0 14px;
}
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none;
  min-height: 42px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 19px;
  white-space: nowrap;
  transition: border-color 0.15s, background 0.15s;
}
.chips button[aria-checked="true"] { border-color: var(--accent); background: var(--surface-2); }

.swatches { display: flex; gap: 6px; flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; padding: 3px; margin: -3px; }
.swatches::-webkit-scrollbar { display: none; }
.swatches button {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.18);
  transition: transform 0.12s;
}
.swatches button[aria-checked="true"] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); transform: scale(1.05); }

/* ---------- 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.is-active { color: var(--text); background: var(--surface-2); }
.tool.is-active svg { color: var(--accent); }
.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%); } }

/* Petits écrans en hauteur (iPhone SE, mode paysage) */
@media (max-height: 640px) {
  .panel-body { gap: 6px; padding: 8px 0 6px; }
  .slider { min-height: 34px; }
  .tool { min-height: 50px; }
}

@media (min-width: 700px) {
  .panel-body { max-width: 640px; margin: 0 auto; }
  .toolbar { grid-template-columns: repeat(4, 110px); justify-content: center; }
}
