:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --line: #e2e5ea;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --danger: #c2410c;
  --stage: #d9dce2;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c; --panel: #1e2128; --line: #2e323b; --text: #e6e8ec;
    --muted: #9aa1ad; --accent: #5b8cff; --accent-soft: #23304d; --danger: #f08a5d; --stage: #0f1115;
  }
}
:root[data-theme="dark"] {
  --bg: #15171c; --panel: #1e2128; --line: #2e323b; --text: #e6e8ec;
  --muted: #9aa1ad; --accent: #5b8cff; --accent-soft: #23304d; --danger: #f08a5d; --stage: #0f1115;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); display: flex; flex-direction: column; font-size: 14px; }

button, .btn {
  font: inherit; font-size: 13px; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px; white-space: nowrap;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button:disabled { opacity: .45; cursor: default; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
.brand small { font-weight: 400; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.app { flex: 1; display: grid; grid-template-columns: 290px 1fr 270px; min-height: 0; }
.panel { background: var(--panel); overflow-y: auto; padding: 4px 14px 20px; }
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }
.panel section { padding: 12px 0; border-bottom: 1px solid var(--line); }
.panel section:last-child { border-bottom: 0; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; }

.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center;
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 16px 8px; cursor: pointer;
  color: var(--muted); transition: .15s;
}
.dropzone strong { color: var(--text); font-size: 13px; }
.dropzone span { font-size: 12px; }
.dropzone.over, .dropzone:hover { border-color: var(--accent); background: var(--accent-soft); }

.library { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 10px; }
.thumb {
  position: relative; aspect-ratio: 1; border: 2px solid var(--line); border-radius: 50%;
  background: #fff conic-gradient(#eee 25%, #fff 0 50%, #eee 0 75%, #fff 0) 0 0/10px 10px;
  overflow: hidden; cursor: grab;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.thumb.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.thumb .del {
  position: absolute; top: 0; right: 0; width: 18px; height: 18px; padding: 0; border-radius: 50%;
  font-size: 11px; line-height: 1; display: none; background: var(--danger); color: #fff; border: 0;
}
.thumb:hover .del { display: inline-flex; }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.btn-grid button { white-space: normal; text-align: center; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; }
.field output { color: var(--text); font-variant-numeric: tabular-nums; }
.field input[type=number], .field select {
  font: inherit; font-size: 13px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%;
}
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.field input[type=color] { width: 44px; height: 28px; border: 1px solid var(--line); border-radius: 6px; padding: 0; background: none; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; margin: 6px 0; cursor: pointer; }
.check input { accent-color: var(--accent); }
.check.small { font-size: 12px; margin: 0; color: var(--muted); }

.hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; line-height: 1.4; }
.hint.center { text-align: center; margin: 8px 0 10px; }
.info { font-size: 13px; margin: 6px 0; }
.warn { font-size: 12px; color: var(--danger); margin: 6px 0; }
.dup { display: flex; flex-direction: column; gap: 6px; margin: 4px 0 12px; padding-bottom: 12px; border-bottom: 1px dashed var(--line); }
.dup-row { display: flex; gap: 6px; align-items: center; }
.dup-row button { flex: 1; }
.dup-count { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); }
.dup-count input { width: 58px; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px; }
.help { padding-left: 18px; margin: 0; font-size: 13px; line-height: 1.5; }

.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--stage); }
.stage-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 6px 14px; }
.seg.mode { display: flex; margin-bottom: 10px; }
.seg.mode button { flex: 1; }
.seg button.on { background: var(--accent); color: #fff; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.tabs button.on { border-color: var(--accent); background: var(--accent-soft); }
.page-actions { display: flex; gap: 4px; }

.canvas-wrap { flex: 1; min-height: 0; overflow: auto; display: flex; align-items: flex-start; justify-content: center; padding: 16px; }
#pageCanvas { background: #fff; box-shadow: 0 2px 14px rgba(0,0,0,.18); touch-action: none; }
.canvas-wrap.over #pageCanvas { outline: 3px solid var(--accent); }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #1f2430; color: #fff;
  padding: 10px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.25); z-index: 10;
}

@media (max-width: 1100px) {
  .app { grid-template-columns: 260px 1fr; }
  .panel.right { grid-column: 1 / -1; border-left: 0; border-top: 1px solid var(--line); }
}
@media (max-width: 760px) {
  .app { display: flex; flex-direction: column; }
  .panel { overflow: visible; }
  .canvas-wrap { min-height: 70vh; padding: 16px; }
}

/* ---------- Navigation entre modules ---------- */
.back { color: var(--muted); text-decoration: none; font-weight: 500; font-size: 13px; margin-right: 6px; padding: 4px 8px; border-radius: 6px; border: 1px solid var(--line); }
.back:hover { color: var(--text); border-color: var(--accent); }

/* ---------- Tableau de bord ---------- */
body.dashboard { min-height: 100%; }
.dash { max-width: 1040px; width: 100%; margin: 0 auto; padding: 28px 16px 40px; }
.dash h1 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 14px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 14px; }
.card {
  display: flex; gap: 14px; align-items: flex-start; padding: 18px; border-radius: 12px; text-decoration: none;
  color: var(--text); background: var(--panel); border: 1px solid var(--line); transition: .15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-1px); box-shadow: 0 4px 16px rgba(0,0,0,.08); }
.card svg { color: var(--accent); flex: none; }
.card h2 { font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--text); margin: 4px 0 6px; }
.card p { margin: 0; font-size: 13px; line-height: 1.45; color: var(--muted); }
.card .tag { font-size: 11px; color: var(--accent); background: var(--accent-soft); padding: 2px 8px; border-radius: 99px; }
.card.soon { opacity: .55; pointer-events: none; }

/* ---------- Module stickers ---------- */
.sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 2px; }
.library.square .thumb { border-radius: 6px; }
.library.square .thumb img { object-fit: contain; }
[hidden] { display: none !important; }

.thumb .split {
  position: absolute; bottom: 0; right: 0; width: 20px; height: 20px; padding: 0; border-radius: 50%;
  font-size: 11px; line-height: 1; display: none; background: var(--accent); color: #fff; border: 0;
}
.thumb:hover .split { display: inline-flex; }

/* ---------- Outils d'image partagés ---------- */
.image-tools { margin-top: 12px; padding: 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); }
.image-tools .it-head { display: flex; gap: 6px; align-items: baseline; font-size: 12px; margin-bottom: 8px; min-width: 0; }
.image-tools .it-label { color: var(--muted); flex: none; }
.image-tools .it-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.image-tools .btn-grid button:first-child { grid-column: 1 / -1; }

/* ---------- Module tasses ---------- */
.mug-wrap { flex-direction: column; align-items: center; gap: 16px; }
.mug-preview { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; justify-content: center; }
.mug-preview canvas { background: radial-gradient(circle at 50% 40%, #f4f5f7, #d9dce2); border-radius: 12px; max-width: 100%; }
.mug-controls { display: flex; flex-direction: column; gap: 8px; min-width: 160px; }
.design-title { font-weight: 600; font-size: 13px; color: var(--muted); }
.design-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.design-item {
  display: flex; align-items: center; gap: 8px; padding: 4px; border: 2px solid var(--line); border-radius: 8px;
  cursor: pointer; background: var(--panel); text-align: left; font-size: 12px;
}
.design-item.active { border-color: var(--accent); background: var(--accent-soft); }
.design-item canvas { width: 96px; height: auto; border-radius: 3px; flex: none; background: #fff; }
.design-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.layer-item {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px;
  cursor: pointer; font-size: 12px; background: var(--panel);
}
.layer-item.active { border-color: var(--accent); background: var(--accent-soft); }
.layer-item img { width: 26px; height: 26px; object-fit: contain; flex: none; }
.layer-item .lt { width: 26px; text-align: center; font-weight: 700; flex: none; }
.layer-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field textarea { font: inherit; font-size: 13px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; resize: vertical; }
.row-inline { display: flex; gap: 10px; align-items: center; }

/* ---------- Résolution d'impression (alerte) ---------- */
.res { display: inline-block; font-size: 12px; padding: 4px 8px; border-radius: 6px; line-height: 1.35; }
.res-good { background: rgba(22,163,74,.14); color: #15803d; }
.res-medium { background: rgba(245,158,11,.18); color: #b45309; }
.res-low { background: rgba(220,38,38,.14); color: #dc2626; }
.res-vector { background: var(--accent-soft); color: var(--accent); }
.res-line { margin: 0 0 10px; }
.res-line:empty { display: none; }
.res-dot { margin-left: auto; flex: none; width: 18px; height: 18px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px; color: #fff; }
.res-dot.res-low { background: #dc2626; }
.res-dot.res-medium { background: #f59e0b; }
.image-tools .it-res { font-size: 12px; color: var(--muted); margin: -4px 0 8px; }

/* ---------- Aperçu 3D partagé ---------- */
.preview3d { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.preview3d .p3d-canvas { background: radial-gradient(circle at 50% 35%, #f6f7f9, #d6d9df); border-radius: 12px; cursor: grab; touch-action: none; max-width: 100%; height: auto !important; }
.preview3d .p3d-canvas:active { cursor: grabbing; }
.preview3d .p3d-controls { width: 100%; }
.preview3d .p3d-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* ---------- Tasse 3D (WebGL) ---------- */
.mug3d-canvas { border-radius: 14px; background: radial-gradient(circle at 50% 30%, #ffffff, #e3e5e9 60%, #c9ccd3); cursor: grab; touch-action: none; max-width: 100%; height: auto !important; box-shadow: inset 0 0 0 1px rgba(0,0,0,.05); }
.mug3d-canvas:active { cursor: grabbing; }

/* ---------- Rendu 3D WebGL des objets plats ---------- */
.preview3d .p3d-host { position: relative; max-width: 100%; }
.preview3d .p3d-canvas.webgl { background: radial-gradient(circle at 50% 30%, #ffffff, #e3e5e9 60%, #c9ccd3); }
.preview3d .p3d-label { position: absolute; left: 0; right: 0; bottom: 6px; text-align: center; font-size: 11px; font-weight: 600; color: #6b7280; pointer-events: none; }
.preview3d .p3d-row { flex-wrap: wrap; }

/* ---------- Colorisation du fond ---------- */
.bg-colors { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.bg-pick { border: 2px solid transparent; border-radius: 8px; padding: 4px 6px; cursor: pointer; margin: 0; }
.bg-pick.on { border-color: var(--accent); background: var(--accent-soft); }
.swatches { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; }
.swatch { aspect-ratio: 1; padding: 0; border-radius: 5px; border: 1px solid rgba(0,0,0,.18); min-width: 0; }
.swatch:hover { transform: scale(1.12); }
#btnPipette.on { border-color: var(--accent); background: var(--accent-soft); }

.bg-panel .bg-colors { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.bg-panel .bg-sub { margin: 8px 0 4px; }
.bg-panel .bg-actions { margin-top: 8px; }
.bg-panel .bg-pipette.on { border-color: var(--accent); background: var(--accent-soft); }
.bg-panel .bg-empty { margin: 0; }
