:root {
  --bg: #f6f3ec;
  --card: #ffffff;
  --ink: #1d1b16;
  --muted: #6f6a5f;
  --line: #e6e0d3;
  --yellow: #ffcf00;
  --yellow-ink: #3a2c00;
  --red: #d01012;
  --blue: #006cb7;
  --green: #1f9d55;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(40 30 0 / .06), 0 4px 14px rgb(40 30 0 / .06);
  --tab-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15140f; --card: #211f18; --ink: #f2eee4; --muted: #a39d8f; --line: #353126;
    --shadow: 0 1px 2px rgb(0 0 0 / .3); color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--blue); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .4em; }
h2 { font-size: 1.2rem; } h3 { font-size: 1.02rem; display: flex; align-items: center; gap: 8px; }
.muted { color: var(--muted); margin: 0 0 12px; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.center { text-align: center; }
.error { color: var(--red); }

/* ---------- boutons & champs ---------- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer; border: 1.5px solid var(--line); background: var(--card);
  color: var(--ink); border-radius: 12px; padding: 9px 14px; display: inline-flex; align-items: center;
  justify-content: center; gap: 6px; text-decoration: none; min-height: 42px;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--yellow); border-color: #e8b900; color: var(--yellow-ink); box-shadow: inset 0 -3px 0 rgb(0 0 0 / .12); }
.btn.danger { color: var(--red); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.big { width: 100%; min-height: 50px; font-size: 16px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { font: inherit; font-size: 22px; background: none; border: 0; color: inherit; cursor: pointer; width: 40px; height: 40px; border-radius: 10px; }
input, select {
  font: inherit; color: var(--ink); background: var(--bg); border: 1.5px solid var(--line); border-radius: 10px;
  padding: 9px 11px; width: 100%; min-height: 42px;
}
input:focus, select:focus { outline: 3px solid rgb(255 207 0 / .45); border-color: #e8b900; }
input[type=range] { padding: 0; min-height: 28px; accent-color: #e8b900; background: none; border: 0; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.field.wide { grid-column: 1 / -1; }
.field.slider b { color: var(--ink); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; margin-bottom: 12px; }
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 600; grid-column: 1 / -1; min-height: 36px; }
.switch input { display: none; }
.switch span { width: 44px; height: 26px; border-radius: 99px; background: var(--line); position: relative; flex: none; transition: .2s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / .3); transition: .2s; }
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { left: 21px; }
.pill { font-size: 12px; font-weight: 700; background: var(--bg); border: 1px solid var(--line); border-radius: 99px; padding: 2px 9px; color: var(--muted); }

.seg { display: inline-flex; background: var(--bg); border: 1.5px solid var(--line); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button { font: inherit; font-weight: 600; font-size: 14px; border: 0; background: none; color: var(--muted); padding: 7px 12px; border-radius: 9px; cursor: pointer; }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.seg.small button { padding: 5px 10px; font-size: 13px; }

.chips { display: flex; gap: 6px; }
.scroll-x { overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chip { font: inherit; font-size: 13px; font-weight: 600; white-space: nowrap; border: 1.5px solid var(--line); background: var(--card); color: var(--ink); border-radius: 99px; padding: 5px 10px 5px 6px; display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.chip.on { border-color: var(--ink); }
.dot { width: 16px; height: 16px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgb(0 0 0 / .18); display: inline-block; }

/* ---------- connexion ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(circle at 20% 10%, #ffe98a 0, transparent 40%), radial-gradient(circle at 90% 90%, #ffc2c2 0, transparent 35%), var(--bg); }
.login-card { width: 100%; max-width: 380px; background: var(--card); border-radius: 22px; padding: 28px 22px; box-shadow: var(--shadow); text-align: center; }
.login-card form { display: grid; gap: 10px; margin-top: 16px; }
.brick-logo { width: 96px; height: 48px; margin: 0 auto 14px; background: var(--red); border-radius: 8px; position: relative; display: flex; justify-content: space-around; align-items: center; padding: 0 8px; box-shadow: inset 0 -5px 0 rgb(0 0 0 / .18); }
.brick-logo i { width: 16px; height: 16px; border-radius: 50%; background: #e8292b; box-shadow: inset 0 -2px 0 rgb(0 0 0 / .2), 0 0 0 2px rgb(255 255 255 / .12); }

/* ---------- structure ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 6px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 19px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
.sync { color: var(--muted); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
main { padding: 14px 16px calc(var(--tab-h) + 24px + env(safe-area-inset-bottom)); max-width: 1100px; margin: 0 auto; }
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabbar button { font: inherit; font-size: 12px; font-weight: 600; background: none; border: 0; color: var(--muted); height: var(--tab-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; cursor: pointer; }
.tabbar button span { font-size: 22px; filter: grayscale(1); opacity: .7; }
.tabbar button.on { color: var(--ink); }
.tabbar button.on span { filter: none; opacity: 1; }
@media (min-width: 900px) {
  .tabbar { top: 0; bottom: auto; left: auto; right: 16px; width: 440px; border: 0; background: none; height: 58px; }
  .tabbar button { height: 58px; flex-direction: row; font-size: 14px; }
  .tabbar button span { font-size: 18px; }
  .topbar { padding-right: 470px; }
  main { padding-bottom: 40px; }
}

.toolbar { display: flex; gap: 8px; margin-bottom: 14px; }
.panel { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-art { font-size: 56px; }
.stock-line { font-weight: 600; margin: 0 0 12px; }

/* ---------- cartes de set ---------- */
.set-card { display: flex; gap: 12px; background: var(--card); border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); cursor: pointer; text-align: left; border: 0; font: inherit; color: inherit; width: 100%; }
.set-card img { width: 96px; height: 80px; object-fit: contain; background: #fff; border-radius: 10px; flex: none; }
.set-card .t { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 3px; }
.set-card b { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.set-card small { color: var(--muted); font-size: 12.5px; }
.progress { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; margin-top: auto; }
.progress i { display: block; height: 100%; width: 0; background: var(--yellow); border-radius: 99px; transition: width .3s; }
.progress.done i { background: var(--green); }
.progress.big { height: 12px; margin-top: 10px; }
.progress-txt { font-weight: 700; margin: 6px 0 0; font-size: 14px; }
.badge { font-size: 11px; font-weight: 700; border-radius: 6px; padding: 1px 6px; background: var(--bg); color: var(--muted); }
.badge.ok { background: #dff5e7; color: #13693a; }
@media (prefers-color-scheme: dark) { .badge.ok { background: #173a26; color: #7fe0a6; } }

/* ---------- détail d'un set ---------- */
.set-hero { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.set-hero img { width: 130px; height: 104px; object-fit: contain; background: #fff; border-radius: 14px; flex: none; box-shadow: var(--shadow); }
.set-hero-txt { flex: 1; min-width: 0; }
.set-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.set-actions .primary { flex: 1; }
.filters { display: grid; gap: 8px; margin-bottom: 12px; position: sticky; top: 61px; z-index: 10; background: var(--bg); padding: 6px 0; }
.filters .seg { justify-self: start; }
.filters > .seg:nth-child(-n+2) { display: inline-flex; }
@media (min-width: 600px) { .filters { grid-template-columns: auto auto 1fr; align-items: center; } .filters #sd-colors { grid-column: 1 / -1; } }

.group-title { display: flex; align-items: center; gap: 8px; margin: 18px 0 8px; font-size: 15px; }
.group-title small { color: var(--muted); font-weight: 600; }
.items { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
.item { position: relative; background: var(--card); border-radius: 12px; padding: 6px 6px 8px; box-shadow: var(--shadow); cursor: pointer; user-select: none; -webkit-user-select: none; border: 2px solid transparent; transition: opacity .2s; touch-action: manipulation; }
.item img { width: 100%; aspect-ratio: 1; object-fit: contain; background: #fff; border-radius: 8px; display: block; pointer-events: none; }
.item .qty { position: absolute; top: 4px; left: 4px; font-weight: 800; font-size: 13px; background: var(--ink); color: var(--bg); border-radius: 8px; padding: 1px 7px; }
.item .minus { position: absolute; top: 2px; right: 2px; width: 30px; height: 30px; border-radius: 9px; border: 0; background: rgb(0 0 0 / .55); color: #fff; font-size: 18px; font-weight: 700; cursor: pointer; line-height: 1; }
.item .cap { font-size: 11.5px; line-height: 1.25; margin-top: 5px; display: flex; gap: 5px; align-items: flex-start; }
.item .cap .dot { width: 11px; height: 11px; margin-top: 2px; }
.item .cap span { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.item .pn { font-size: 10.5px; color: var(--muted); }
.item.partial { border-color: var(--yellow); }
.item.done { opacity: .5; }
.item.done .qty { background: var(--green); color: #fff; }
.item.done::after { content: "✓"; position: absolute; inset: 6px 6px auto; aspect-ratio: 1; display: grid; place-items: center; font-size: 42px; font-weight: 900; color: var(--green); text-shadow: 0 0 6px #fff; pointer-events: none; }
.item.bump { animation: bump .18s; }
@keyframes bump { 50% { transform: scale(.94); } }

/* ---------- construire ---------- */
.idea { display: flex; gap: 12px; background: var(--card); border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); cursor: pointer; border: 0; font: inherit; color: inherit; text-align: left; width: 100%; }
.idea img { width: 110px; height: 88px; object-fit: contain; background: #fff; border-radius: 10px; flex: none; }
.idea .t { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.pct { font-size: 22px; font-weight: 800; }

/* ---------- listes de pièces ---------- */
.part-rows { display: grid; gap: 2px; }
.part-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); }
.part-row img { width: 44px; height: 44px; object-fit: contain; background: #fff; border-radius: 8px; flex: none; }
.part-row .sw { width: 30px; height: 30px; border-radius: 8px; flex: none; box-shadow: inset 0 0 0 1px rgb(0 0 0 / .15), inset 0 -3px 0 rgb(0 0 0 / .12); }
.part-row .t { flex: 1; min-width: 0; font-size: 13.5px; }
.part-row .t small { display: block; color: var(--muted); font-size: 12px; }
.part-row .n { font-weight: 800; white-space: nowrap; }
.export-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }

/* ---------- feuille ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgb(0 0 0 / .45); }
.sheet-body { position: relative; background: var(--card); width: 100%; max-width: 640px; max-height: 88vh; overflow: auto; border-radius: 22px 22px 0 0; padding: 0 16px calc(20px + env(safe-area-inset-bottom)); animation: up .22s ease-out; }
@media (min-width: 700px) { .sheet { align-items: center; } .sheet-body { border-radius: 22px; } }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-head { position: sticky; top: 0; background: var(--card); display: flex; align-items: center; padding: 14px 0 8px; z-index: 1; }
.sheet-head h2 { flex: 1; margin: 0; }
.search-results { display: grid; gap: 8px; margin-top: 12px; }
.menu-list { display: grid; gap: 8px; }

/* ---------- mosaïque ---------- */
.file-btn { cursor: pointer; margin: 6px 0 4px; }
.saved-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin-top: 14px; }
.saved { background: var(--bg); border-radius: 12px; padding: 6px; cursor: pointer; position: relative; border: 0; font: inherit; color: inherit; text-align: left; }
.saved img { width: 100%; aspect-ratio: 1; object-fit: contain; border-radius: 8px; display: block; background: #222; image-rendering: pixelated; }
.saved b { font-size: 13px; display: block; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mo-layout { display: grid; gap: 14px; }
@media (min-width: 900px) { .mo-layout { grid-template-columns: minmax(0, 1fr) 360px; align-items: start; } .mo-stage { position: sticky; top: 76px; } }
.mo-stage { display: grid; gap: 10px; }
.crop-wrap, .preview-wrap { background: #1b1a17; border-radius: var(--radius); overflow: hidden; display: grid; place-items: center; touch-action: none; }
.crop-wrap canvas { width: 100%; height: auto; display: block; cursor: grab; }
.preview-wrap canvas { width: 100%; height: auto; display: block; }
.actions-col { display: grid; gap: 8px; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + 18px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; opacity: 0; pointer-events: none; transition: .25s; z-index: 60; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- impression du plan ---------- */
#print-root { display: none; }
@media print {
  @page { size: A4; margin: 10mm; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; color: #000; background: #fff; font: 10pt system-ui, sans-serif; }
  .pr-page { page-break-after: always; break-after: page; }
  .pr-page:last-child { page-break-after: auto; }
  .pr-page h2 { font-size: 14pt; margin: 0 0 4mm; }
  .pr-legend { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.5mm 4mm; margin-bottom: 4mm; font-size: 8pt; }
  .pr-legend div { display: flex; align-items: center; gap: 2mm; }
  .pr-legend i { width: 5mm; height: 5mm; border: .2mm solid #555; display: inline-grid; place-items: center; font-style: normal; font-size: 7pt; font-weight: 700; }
  .pr-grid { border-collapse: collapse; margin: 0 auto; }
  .pr-grid td { width: 10.5mm; height: 10.5mm; border: .2mm solid #999; text-align: center; font-size: 8pt; font-weight: 700; padding: 0; }
  .pr-grid th { font-size: 7pt; color: #555; font-weight: 400; }
  .pr-overview img { width: 100%; max-height: 150mm; object-fit: contain; image-rendering: pixelated; }
  .pr-map { border-collapse: collapse; margin-top: 4mm; }
  .pr-map td { border: .3mm solid #333; padding: 2mm 4mm; text-align: center; font-weight: 700; }
}
