:root {
  --bg: #ffffff;
  --ink: #111111;
  --ink-2: #5c5c5c;
  --ink-3: #9a9a9a;
  --line: #e7e7e7;
  --line-2: #d4d4d4;
  --hover: #f3f3f3;
  --panel: rgba(255, 255, 255, 0.92);
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 12px 40px -8px rgba(0, 0, 0, .22);
  --accent: #ff4b2b;
  --accent-ink: #ffffff;
  --danger: #d92d20;
  --checker: #eeeeee;
  --radius: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #121212;
  --ink: #f2f2f2;
  --ink-2: #b0b0b0;
  --ink-3: #777777;
  --line: #2a2a2a;
  --line-2: #3a3a3a;
  --hover: #242424;
  --panel: rgba(28, 28, 28, 0.94);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 16px 48px -8px rgba(0, 0, 0, .7);
  --danger: #ff6b5e;
  --checker: #1d1d1d;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

button { font: inherit; color: inherit; }
kbd {
  font: 11px var(--mono);
  color: var(--ink-3);
}
[hidden] { display: none !important; }

/* ---------- Zone de dépôt ---------- */

.drop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  outline: none;
  cursor: pointer;
}

.drop-box {
  width: min(520px, calc(100vw - 32px));
  aspect-ratio: 16 / 10;
  border: 1.5px dashed var(--line-2);
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 24px;
  text-align: center;
  transition: border-color .2s, background-color .2s, transform .2s;
}

.drop:hover .drop-box,
.drop:focus-visible .drop-box {
  border-color: var(--ink-3);
}

.drop-icon {
  width: 36px;
  height: 36px;
  color: var(--ink-3);
  margin-bottom: 10px;
}

.drop-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -.01em;
}

.drop-sub {
  margin: 0;
  color: var(--ink-2);
}

.drop-sub kbd {
  display: inline-block;
  padding: 1px 5px;
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--ink-2);
}

.drop-note {
  margin: 14px 0 0;
  max-width: 46ch;
  color: var(--danger);
  font-size: 13px;
}

.link {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: var(--line-2);
}

.drop-hint {
  position: fixed;
  bottom: 18px;
  left: 0;
  right: 0;
  margin: 0;
  text-align: center;
  color: var(--ink-3);
  font-size: 12px;
}

/* ---------- Glisser-déposer ---------- */

.drag-overlay {
  position: fixed;
  inset: 12px;
  z-index: 50;
  border: 2px dashed var(--accent);
  border-radius: 18px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  pointer-events: none;
}

.drag-overlay span {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
}

/* ---------- Éditeur ---------- */

.editor {
  position: fixed;
  inset: 0;
}

.editor > img {
  display: block;
  max-width: 100%;
}

.cropper-container { background: var(--bg); }

.cropper-canvas {
  background-color: var(--checker);
  background-image:
    linear-gradient(45deg, var(--bg) 25%, transparent 25%),
    linear-gradient(-45deg, var(--bg) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--bg) 75%),
    linear-gradient(-45deg, transparent 75%, var(--bg) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

.cropper-view-box { outline: 1px solid var(--accent); outline-color: var(--accent); }
.cropper-line { background-color: var(--accent); }
.cropper-point { background-color: var(--accent); width: 7px; height: 7px; opacity: 1; }
.cropper-point.point-se { width: 7px; height: 7px; }
.cropper-modal { background-color: #000; opacity: .45; }

.editor.pixelated .cropper-canvas img,
.editor.pixelated .cropper-view-box img {
  image-rendering: pixelated;
}

/* ---------- Barre d'état ---------- */

.status {
  position: fixed;
  left: 14px;
  right: 14px;
  bottom: 10px;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
  font: 11px var(--mono);
  color: var(--ink-2);
}

.status span {
  background: var(--panel);
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Aperçu de la sélection ---------- */

.preview {
  --stage-w: 240px;
  --stage-h: 160px;
  position: fixed;
  right: 14px;
  bottom: 40px;
  z-index: 20;
  width: calc(var(--stage-w) + 2px);
  background: var(--panel);
  backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.preview-head {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 28px;
  padding: 0 4px 0 10px;
  cursor: grab;
  touch-action: none;
}
.preview.dragging .preview-head { cursor: grabbing; }

.preview-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-2);
}

.preview-size {
  flex: 1;
  font: 11px var(--mono);
  color: var(--ink-3);
  text-align: right;
  white-space: nowrap;
}

.preview-toggle {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--ink-2);
  cursor: pointer;
}
.preview-toggle:hover { background: var(--hover); color: var(--ink); }
.preview-toggle svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform .15s;
}
.preview.collapsed .preview-toggle svg { transform: rotate(180deg); }

.preview-stage {
  display: grid;
  place-items: center;
  height: var(--stage-h);
  border-top: 1px solid var(--line);
  overflow: hidden;
  background-color: var(--checker);
  background-image:
    linear-gradient(45deg, var(--bg) 25%, transparent 25%),
    linear-gradient(-45deg, var(--bg) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--bg) 75%),
    linear-gradient(-45deg, transparent 75%, var(--bg) 75%);
  background-size: 12px 12px;
  background-position: 0 0, 0 6px, 6px -6px, -6px 0;
}

/* Réduit : la scène est masquée sans display:none, Cropper.js mesure .preview-crop au chargement */
.preview.collapsed .preview-stage { height: 0; border-top-width: 0; }

.preview-crop,
.preview-full {
  grid-area: 1 / 1;
}

.preview-crop {
  width: var(--stage-w);
  height: var(--stage-h);
  overflow: hidden;
}

.preview-full {
  max-width: var(--stage-w);
  max-height: var(--stage-h);
}

/* ---------- Menu contextuel ---------- */

.menu {
  position: fixed;
  z-index: 100;
  width: 568px;
  max-width: calc(100vw - 16px);
  max-height: calc(100vh - 16px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px;
  background: var(--panel);
  backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  transform-origin: top left;
  animation: menu-in .12s ease-out;
  scrollbar-width: thin;
}

@keyframes menu-in {
  from { opacity: 0; transform: scale(.97); }
}

.menu-head {
  padding: 8px 10px 9px;
  border-bottom: 1px solid var(--line);
  margin: 0 -4px 0;
}

.menu-file {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu-meta {
  color: var(--ink-3);
  font: 11px var(--mono);
  margin-top: 2px;
}

.menu-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.menu-col { min-width: 0; padding: 0 4px; }
.menu-col + .menu-col { border-left: 1px solid var(--line); }

.menu-group {
  padding: 4px 0;
  border-bottom: 1px solid var(--line);
}
.menu-group:last-child { border-bottom: 0; }

@media (max-width: 600px) {
  .menu { width: 284px; }
  .menu-cols { grid-template-columns: 1fr; }
  .menu-col + .menu-col { border-left: 0; border-top: 1px solid var(--line); }
}

.label {
  padding: 6px 8px 4px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  height: 28px;
  padding: 0 8px;
  border: 0;
  border-radius: 6px;
  background: none;
  text-align: left;
  cursor: pointer;
}

.item span { flex: 1; }
.item:hover, .item:focus-visible { background: var(--hover); outline: none; }
.item.danger { color: var(--danger); }

svg {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.item svg { color: var(--ink-2); }
.item.danger svg { color: inherit; }

.disabled-group,
.item[disabled],
.tool[disabled],
.chip[disabled] {
  opacity: .35;
  pointer-events: none;
}

.tools {
  display: flex;
  gap: 4px;
  padding: 0 4px 4px;
}

.tool {
  display: grid;
  place-items: center;
  flex: 1;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: none;
  cursor: pointer;
  color: var(--ink);
}
.tool:hover { background: var(--hover); }
.tool.sm { flex: none; width: 26px; height: 26px; }

.ratios {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 0 4px 6px;
}

.ratio {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  height: 42px;
  padding: 5px 0 3px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: none;
  cursor: pointer;
  font: 11px var(--mono);
  color: var(--ink-2);
}
.ratio:hover { background: var(--hover); }
.ratio i {
  display: block;
  border: 1.5px solid currentColor;
  border-radius: 2px;
}
.ratio.free i { border-style: dashed; }
.ratio[aria-checked="true"] {
  border-color: var(--ink);
  color: var(--ink);
  background: var(--hover);
}
.ratio kbd { display: none; }

.segmented {
  display: flex;
  margin: 0 4px 6px;
  padding: 2px;
  border-radius: 8px;
  background: var(--hover);
}
.segmented button {
  flex: 1;
  height: 26px;
  border: 0;
  border-radius: 6px;
  background: none;
  cursor: pointer;
  color: var(--ink-2);
  font-weight: 500;
}
.segmented button[aria-checked="true"] {
  background: var(--bg);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12);
}

.slider-row {
  display: grid;
  grid-template-columns: 74px 1fr 62px;
  align-items: center;
  gap: 8px;
  height: 28px;
  padding: 0 8px;
  color: var(--ink-2);
}
.slider-row output {
  text-align: right;
  font: 11px var(--mono);
  color: var(--ink);
}
.slider-row.off { opacity: .35; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 16px;
  background: none;
  margin: 0;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 3px;
  background: linear-gradient(var(--ink), var(--ink)) 0 / var(--p, 50%) 100% no-repeat, var(--line-2);
}
input[type="range"]::-moz-range-track {
  height: 3px;
  border-radius: 3px;
  background: var(--line-2);
}
input[type="range"]::-moz-range-progress {
  height: 3px;
  border-radius: 3px;
  background: var(--ink);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--bg);
  border: 1.5px solid var(--ink);
}
input[type="range"]::-moz-range-thumb {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--bg);
  border: 1.5px solid var(--ink);
}

.estimate {
  padding: 2px 8px 6px;
  font: 11px var(--mono);
  color: var(--ink-3);
}
.estimate.heavy { color: var(--accent); }

.preset-field {
  position: relative;
  display: flex;
  gap: 4px;
  padding: 0 4px 4px;
}
.preset-field input {
  flex: 1;
  min-width: 0;
  height: 30px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  user-select: text;
}
.preset-field input:focus { outline: none; border-color: var(--ink-3); }
.preset-save {
  height: 30px;
  padding: 0 10px;
  border: 0;
  border-radius: 7px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 600;
  cursor: pointer;
}

.preset-list { padding: 0 0 2px; }
.preset-list:empty { display: none; }

.preset {
  display: flex;
  align-items: center;
  height: 28px;
  padding: 0 4px 0 8px;
  border-radius: 6px;
  cursor: pointer;
}
.preset:hover { background: var(--hover); }
.preset .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preset .desc { font: 10.5px var(--mono); color: var(--ink-3); margin-right: 4px; }
.preset .del {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 5px;
  background: none;
  color: var(--ink-3);
  cursor: pointer;
  visibility: hidden;
}
.preset:hover .del { visibility: visible; }
.preset .del:hover { color: var(--danger); background: var(--bg); }
.preset .del svg { width: 12px; height: 12px; }
.preset-empty { padding: 4px 8px 6px; color: var(--ink-3); font-size: 12px; }

.zoom-row {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 32px;
  padding: 0 4px 0 8px;
}
.zoom-row > span { flex: 1; color: var(--ink-2); }
.zoom-row output {
  min-width: 52px;
  text-align: center;
  font: 11px var(--mono);
}

.chip {
  height: 26px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: none;
  cursor: pointer;
  font-size: 12px;
}
.chip:hover { background: var(--hover); }

/* ---------- Aide ---------- */

.help {
  width: min(380px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.help::backdrop { background: rgba(0, 0, 0, .25); }
.help-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 14px 10px 18px;
}
.help h2 { margin: 0; font-size: 15px; }
.help dl {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 16px;
  margin: 0;
  padding: 4px 18px 12px;
}
.help dt { color: var(--ink-2); }
.help dd { margin: 0; text-align: right; }
.help dd kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--ink);
}
.help-note {
  margin: 0;
  padding: 10px 18px 16px;
  border-top: 1px solid var(--line);
  color: var(--ink-3);
  font-size: 12px;
}

/* ---------- Notifications ---------- */

.toasts {
  position: fixed;
  left: 50%;
  top: 16px;
  z-index: 200;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.toast {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 12.5px;
  font-weight: 500;
  box-shadow: var(--shadow);
  animation: toast-in .18s ease-out;
  transition: opacity .3s, transform .3s;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transform: translateY(-6px); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-6px); }
}

@media (prefers-reduced-motion: reduce) {
  .menu, .toast { animation: none; }
}
