:root {
  --bg: #f6f4ee; --fg: #1f2a1c; --muted: #6b7466; --card: #ffffff; --line: #dcdcd0;
  --accent: #3f6b35; --accent-fg: #fff; --warn: #b3261e; --sel: #ffb300;
  --panel: rgba(255, 255, 255, 0.96);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #141812; --fg: #e8ecdf; --muted: #9aa392; --card: #1e241b; --line: #33402e; --accent: #7fb069; --accent-fg: #10170d; --panel: rgba(24, 30, 22, 0.97); }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--fg); -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
h1 { font-size: 30px; margin: 8px 0; letter-spacing: -0.5px; }
h2 { font-size: 19px; margin: 24px 0 10px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); color: var(--fg); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.btn:active { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--warn); }
.btn.ghost { background: rgba(0, 0, 0, .35); color: #fff; border-color: rgba(255, 255, 255, .4); }
.btn.grow { flex: 1; }
.icon-btn { border: 0; background: transparent; color: var(--fg); font-size: 19px; width: 38px; height: 38px; border-radius: 10px; cursor: pointer; flex: none; }
.icon-btn.on { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.pad { padding: 10px 12px; }

/* accueil */
#home { max-width: 860px; margin: 0 auto; padding: 24px 18px 30px; }
.hero { text-align: center; padding: 18px 0 8px; }
.logo { display: block; margin: 0 auto; }
.lead { font-size: 16px; max-width: 640px; margin: 8px auto 18px; }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; cursor: pointer; position: relative; }
.card img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; background: #7a9a5a; }
.card .ph { width: 100%; aspect-ratio: 4/3; display: grid; place-items: center; font-size: 40px; background: color-mix(in srgb, var(--accent) 25%, var(--card)); }
.card .meta { padding: 8px 10px; }
.card .meta b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .del { position: absolute; top: 6px; right: 6px; background: rgba(0, 0, 0, .55); color: #fff; border: 0; border-radius: 8px; width: 30px; height: 30px; cursor: pointer; }
.how ol { padding-left: 20px; }
.how li { margin: 6px 0; }
.foot { text-align: center; margin-top: 30px; font-size: 12px; }
.foot a { color: var(--muted); text-decoration: none; }
body.embed .foot { display: none; }

/* caméra */
#camera { position: fixed; inset: 0; background: #000; }
#video { width: 100%; height: 100%; object-fit: contain; }
.cam-ui { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 18px calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; align-items: center; gap: 14px; background: linear-gradient(transparent, rgba(0, 0, 0, .5)); }
.cam-row { display: flex; align-items: center; justify-content: space-between; width: 100%; max-width: 420px; }
.cam-row .spacer { width: 90px; }
.shoot { width: 74px; height: 74px; border-radius: 50%; border: 5px solid #fff; background: rgba(255, 255, 255, .25); cursor: pointer; }
.shoot:active { background: #fff; }
.pill { background: rgba(0, 0, 0, .55); color: #fff; padding: 6px 12px; border-radius: 99px; font-size: 14px; }

/* éditeur */
#editor { position: fixed; inset: 0; display: flex; flex-direction: column; background: #111; }
.topbar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--line); }
.proj-name { flex: 1; min-width: 60px; border: 0; background: transparent; color: var(--fg); font: inherit; font-weight: 700; padding: 6px; border-radius: 8px; }
.proj-name:focus { outline: 2px solid var(--accent); }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex: none; }
.seg button { border: 0; background: transparent; color: var(--fg); padding: 7px 10px; font: inherit; font-weight: 600; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-fg); }
.seg button:disabled { opacity: .4; cursor: default; }
#stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #0d0f0c; }
.stage-canvas { position: absolute; touch-action: none; display: block; cursor: crosshair; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.hint { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 3; background: rgba(0, 0, 0, .62); color: #fff; padding: 7px 13px; border-radius: 99px; font-size: 13px; pointer-events: none; max-width: 92%; text-align: center; }
.bottom { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); max-height: 46vh; overflow: auto; }
.bottom > div { max-width: 760px; margin: 0 auto; }
.sl { display: grid; grid-template-columns: 112px 1fr 84px; align-items: center; gap: 8px; margin: 2px 0; font-size: 13px; }
.sl span { color: var(--muted); }
.sl output { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.sel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 4px; }
.chip-placing { flex: 1; display: flex; align-items: center; justify-content: space-between; gap: 6px; background: color-mix(in srgb, var(--sel) 30%, var(--card)); border: 1px solid var(--sel); border-radius: 12px; padding: 4px 4px 4px 12px; font-weight: 600; min-height: 42px; }
.chip-placing button { border: 0; background: rgba(0, 0, 0, .12); color: var(--fg); width: 34px; height: 34px; border-radius: 9px; cursor: pointer; font-size: 16px; }

.overlay { position: absolute; inset: 0; z-index: 5; background: var(--bg); display: flex; flex-direction: column; }
.search-head { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); justify-content: space-between; }
.search-head input { flex: 1; font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 10px; flex: none; scrollbar-width: none; }
.chips button { flex: none; border: 1px solid var(--line); background: var(--card); color: var(--fg); border-radius: 99px; padding: 5px 11px; font: inherit; font-size: 13px; cursor: pointer; }
.chips button.on { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.results { flex: 1; overflow-y: auto; padding: 0 6px 10px; }
.res { display: flex; gap: 10px; align-items: center; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--fg); padding: 9px 6px; font: inherit; cursor: pointer; }
.res:active { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.res .ico { font-size: 22px; width: 30px; text-align: center; flex: none; }
.res .txt { flex: 1; min-width: 0; }
.res .txt b { display: block; }
.res .txt em { color: var(--muted); font-size: 13px; }
.res .txt .small { display: block; }
.dots { display: flex; gap: 3px; flex: none; }
.dot { width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgba(0, 0, 0, .2); }
.dot.leaf { border-radius: 3px 10px; }
.count { font-weight: 700; min-width: 38px; text-align: right; }
.empty { padding: 20px; text-align: center; color: var(--muted); }

dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--bg); color: var(--fg); max-width: 520px; width: calc(100% - 24px); padding: 16px; }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog h2 { margin-top: 0; }
dialog label { display: flex; flex-direction: column; gap: 3px; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
dialog label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--fg); }
dialog input:not([type=checkbox]), dialog select { font: inherit; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--fg); min-height: 38px; }
dialog input[type=color] { padding: 2px; height: 38px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

@media (hover: hover) {
  .btn:hover { border-color: var(--accent); }
  .icon-btn:hover { background: color-mix(in srgb, var(--accent) 18%, transparent); }
  .res:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
}

/* écrans tactiles (téléphones, tablettes) : cibles plus grandes */
@media (pointer: coarse) {
  .icon-btn { width: 44px; height: 44px; }
  .seg button { padding: 9px 13px; }
  .btn { min-height: 44px; }
  .sl { min-height: 40px; }
  input[type=range] { height: 32px; }
  .chips button { padding: 8px 14px; }
  .res { padding: 12px 6px; }
  .card .del { width: 36px; height: 36px; }
}

/* tablette en portrait : panneau du bas plus aéré, la scène garde la majorité de l'écran */
@media (min-width: 600px) and (orientation: portrait) {
  body { font-size: 16px; }
  .bottom { max-height: 38vh; padding: 12px 20px calc(12px + env(safe-area-inset-bottom)); }
  .sl { grid-template-columns: 150px 1fr 96px; font-size: 14px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
  .hint { font-size: 14px; }
  #home { padding-top: 40px; }
}

/* paysage (tablette, grand téléphone) et ordinateur : panneau latéral */
@media (min-width: 900px), (orientation: landscape) and (min-width: 640px) {
  #editor { display: grid; grid-template-columns: 1fr clamp(290px, 34vw, 400px); grid-template-rows: auto 1fr; }
  .topbar { grid-column: 1 / 3; }
  .bottom { grid-column: 2; grid-row: 2; max-height: none; border-top: 0; border-left: 1px solid var(--line); padding-left: max(12px, env(safe-area-inset-right)); padding-right: max(12px, env(safe-area-inset-right)); }
  .bottom .sl { grid-template-columns: 100px 1fr 72px; }
  #stage { grid-column: 1; grid-row: 2; }
  /* recherche et liste en volet : on garde la vue du massif à côté */
  #stage .overlay { left: auto; width: min(440px, 100%); box-shadow: -8px 0 24px rgba(0, 0, 0, .3); }
  .cam-row { max-width: 520px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .topbar { padding-top: 4px; padding-bottom: 4px; }
  .cam-ui { gap: 8px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .shoot { width: 62px; height: 62px; }
}
/* petits téléphones : la barre du haut doit tenir sur une ligne */
@media (max-width: 480px) {
  .topbar { gap: 0; padding-left: 4px; padding-right: 4px; }
  .topbar .icon-btn { width: 36px; font-size: 17px; }
  .seg button { padding: 7px 8px; }
  .proj-name { min-width: 40px; }
}

.linkbtn { background: none; border: 0; padding: 4px 0; font: inherit; color: var(--accent, inherit); text-decoration: underline; cursor: pointer; }
dialog h3 { margin: 14px 0 6px; font-size: 16px; }
ol.guide { padding-left: 20px; margin: 6px 0; }
ol.guide li { margin: 0 0 7px; line-height: 1.4; }
#guideDlg { max-height: calc(100dvh - 24px); overflow: auto; }
.cam-row .spacer { display: flex; justify-content: flex-end; }
.pill.ok { background: rgba(46, 125, 50, .85); }
.pill.warn { background: rgba(230, 120, 0, .85); }
.pm { border: 1px solid var(--line); border-radius: 12px; padding: 6px 10px 10px; margin: 6px 0 12px; }
.pm legend { font-weight: 600; padding: 0 4px; }
.pm p { margin: 2px 0 8px; }
.pm-list { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.pm-list:empty { display: none; }
.pm-item { position: relative; width: 76px; height: 76px; border-radius: 10px; border: 1px solid var(--line); background: repeating-conic-gradient(#8882 0 25%, transparent 0 50%) 0 0 / 14px 14px; padding: 0; cursor: pointer; overflow: hidden; }
.pm-item canvas { width: 100%; height: 100%; object-fit: contain; display: block; }
.pm-item .n { position: absolute; left: 4px; top: 2px; font-size: 12px; font-weight: 700; color: #fff; text-shadow: 0 0 3px #000; }
.pm-item .x { position: absolute; right: 2px; top: 2px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.55); color: #fff; font-size: 13px; line-height: 22px; padding: 0; cursor: pointer; }
.pm-item.todo { outline: 2px solid var(--sel); }
.cut-dlg { max-width: 960px; padding: 10px; }
.cut-head { margin-bottom: 6px; }
.cut-stage { display: grid; place-items: center; background: #000; border-radius: 10px; overflow: hidden; }
.cut-stage canvas { display: block; max-width: 100%; max-height: min(62vh, 640px); touch-action: none; cursor: crosshair; }
.cut-tools { margin: 8px 0; }
.cut-tools input[type=range] { width: 110px; flex: 1; }
