:root {
  --bg: #eef0ea;
  --panel: #ffffff;
  --ink: #24302a;
  --muted: #6a6a6a;
  --line: #d9ddd3;
  --green: #4f7f42;
  --green-d: #3f6b3a;
  --accent: #2f8ac4;
  --danger: #c0392b;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

/* ---- Barre supérieure ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .04);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo { font-size: 28px; }
.brand h1 { font-size: 18px; margin: 0; color: var(--green-d); }
.brand .sub { margin: 0; font-size: 12px; color: var(--muted); }

.actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.save-info { font-size: 12px; color: var(--muted); }
.save-info code { background: #f0f1ec; padding: 2px 6px; border-radius: 5px; color: var(--ink); }

.status { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: #f0f1ec; color: var(--muted); }
.status.ok { background: #e5f3e1; color: #2f6d33; }
.status.dirty { background: #fff2d8; color: #93690f; }
.status.error { background: #fbe3e0; color: var(--danger); }

/* ---- Boutons ---- */
.btn {
  font: inherit; font-size: 14px; font-weight: 600;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  transition: background .15s, transform .05s, border-color .15s;
}
.btn:hover { background: #f5f6f2; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #446e3a; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #e6b6b0; }
.btn.danger:hover:not(:disabled) { background: #fbeeec; }
.btn.block { width: 100%; }

/* ---- Zone principale ---- */
.main { flex: 1; display: flex; min-height: 0; }

.palette {
  width: 260px; flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 22px;
}
.palette-section h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 0 0 10px;
}
.palette-items { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pal-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 4px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; cursor: pointer; font: inherit; color: var(--ink);
  transition: border-color .15s, background .15s, transform .05s;
}
.pal-item:hover { border-color: var(--green); background: #f6faf4; }
.pal-item:active { transform: translateY(1px); }
.pal-item span { font-size: 11px; text-align: center; line-height: 1.15; color: var(--muted); }

.hints { margin: 12px 0 0; padding-left: 18px; font-size: 12px; color: var(--muted); line-height: 1.6; }
.hints kbd {
  background: #f0f1ec; border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 4px; padding: 0 5px; font-size: 11px; font-family: inherit;
}

/* ---- Canevas ---- */
.canvas-wrap {
  flex: 1; min-width: 0; overflow: auto;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 24px;
  background:
    linear-gradient(45deg, #e7e9e2 25%, transparent 25%, transparent 75%, #e7e9e2 75%) 0 0/22px 22px,
    linear-gradient(45deg, #e7e9e2 25%, transparent 25%, transparent 75%, #e7e9e2 75%) 11px 11px/22px 22px,
    var(--bg);
}
#stage {
  width: min(560px, 100%);
  height: auto;
  background: #f5f2ea;
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .12);
  touch-action: none;
  user-select: none;
  cursor: grab;
}
body.panning, body.panning #stage { cursor: grabbing; }

/* ---- Éléments SVG ---- */
.frame-outline { stroke: #b7b0a2; stroke-width: 2; stroke-dasharray: 6 6; pointer-events: none; }
body.drop-danger .frame-outline { stroke: var(--danger); stroke-dasharray: none; stroke-width: 3; }

.element { cursor: grab; }
.element.dragging { cursor: grabbing; }
.element .sel-ring { stroke: none; }
.element.selected .sel-ring { stroke: var(--accent); stroke-width: 2.5; }
.element.will-delete { opacity: .4; }
.element.will-delete .badge-bg { stroke: var(--danger); }
.badge-label { pointer-events: none; }

/* Zone redimensionnable autour d'un élément (esplanade, restaurant…),
   dessinée dans un calque dédié derrière tous les badges. */
.el-zone {
  fill: #d8ceb4; fill-opacity: .42;
  stroke: #b5652e; stroke-opacity: .7; stroke-width: 1.5; stroke-dasharray: 7 6;
  pointer-events: none;
}
.el-zone.selected { fill-opacity: .58; stroke-opacity: 1; }
.el-zone.will-delete { display: none; }
/* Couleurs spécifiques par type */
.el-zone[data-type="parking"] { fill: #b7bcc1; stroke: #7d838a; }        /* gris */
.el-zone[data-type="parking"].selected { fill-opacity: .62; }
.el-zone[data-type="restaurant"] { fill: #c08a45; fill-opacity: .6; }    /* plus foncé, moins transparent */
.el-zone[data-type="restaurant"].selected { fill-opacity: .8; }
.el-zone-handle { display: none; fill: #fff; stroke: var(--accent); stroke-width: 2; cursor: nwse-resize; }
.el-zone-handle[data-corner="ne"], .el-zone-handle[data-corner="sw"] { cursor: nesw-resize; }
.element.selected .el-zone-handle { display: block; }

/* Chemins : tracé de points déplaçables, insertion en cliquant sur le tracé */
.path-line { fill: none; stroke: #b8975a; stroke-width: 9; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.path-hit { fill: none; stroke: transparent; stroke-width: 20; stroke-linecap: round; stroke-linejoin: round; pointer-events: stroke; cursor: copy; }
.path-body.selected .path-line { stroke: #a07c3e; }
.path-pt { fill: #ffffff; stroke: #b8975a; stroke-width: 2.5; cursor: grab; }
.path-pt:hover { fill: #f4ecd8; }
.path-pt.selected { stroke: var(--accent); }
.path-pt.will-delete-pt { fill: #fbe3e0; stroke: var(--danger); }
.path-pt.merge-hint { fill: #d7f0d4; stroke: #2f8f3a; stroke-width: 3.5; }
.swap-ico { font-size: 15px; }

.divider-hit { fill: transparent; cursor: ns-resize; }
.divider-line { stroke: #ffffff; stroke-width: 2; stroke-dasharray: 6 6; pointer-events: none; opacity: .7; }
.divider-pill { fill: #ffffff; stroke: #9aa38f; stroke-width: 1.5; pointer-events: none; }
.divider:hover .divider-line { opacity: 1; }
.divider:hover .divider-pill { fill: #eaf3e4; }

/* Bouton hamburger (masqué sur grand écran) */
.menu-toggle {
  display: none;
  align-items: center; justify-content: center;
  width: 42px; height: 40px; flex-shrink: 0;
  font-size: 20px; line-height: 1;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
  cursor: pointer;
}
.menu-toggle:active { background: #f0f1ec; }
.drawer-backdrop { display: none; }
.btn-ico { display: none; }

/* ---- Lecture seule ---- */
.ro-badge { display: none; align-items: center; gap: 6px; font-size: 13px; font-weight: 600;
  color: #6a5a2a; background: #fdf3d6; border: 1px solid #ecd9a0; padding: 6px 12px; border-radius: 999px; }
.ro-edit-link { display: none; font-size: 13px; font-weight: 600; color: var(--green-d); text-decoration: none; }
.ro-edit-link:hover { text-decoration: underline; }
body.readonly .palette,
body.readonly .menu-toggle,
body.readonly #status,
body.readonly #btn-share,
body.readonly #btn-reset,
body.readonly #btn-save { display: none; }
body.readonly .ro-badge,
body.readonly .ro-edit-link { display: inline-flex; }
/* Éléments d'édition masqués en lecture seule */
body.readonly .path-pt,
body.readonly .el-zone-handle,
body.readonly .sel-ring,
body.readonly .dividers { display: none; }
body.readonly #stage { width: min(760px, 100%); cursor: grab; }
.ro-error { margin: auto; padding: 24px; text-align: center; color: var(--muted); font-size: 16px; line-height: 1.5; }

/* Encart « lien de partage » */
.share-pop {
  position: fixed; z-index: 80;
  top: calc(var(--topbar-h, 56px) + 8px); right: 14px;
  width: min(420px, calc(100vw - 28px));
  background: #fff; border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .18); padding: 14px;
}
.share-pop[hidden] { display: none; }
.share-pop-title { font-size: 13px; font-weight: 600; color: var(--ink); margin-bottom: 10px; }
.share-pop-title span { color: var(--muted); font-weight: 400; }
.share-pop-row { display: flex; gap: 8px; }
.share-url {
  flex: 1; min-width: 0; font: inherit; font-size: 13px;
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #f7f8f4; color: var(--ink);
}
.share-url:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.share-pop-row .btn { white-space: nowrap; }
.share-pop-hint { min-height: 16px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.share-pop-hint.ok { color: #2f6d33; }

/* ---- Mobile / tablette : palette en tiroir rétractable ---- */
@media (max-width: 820px) {
  .menu-toggle { display: inline-flex; }
  .topbar { gap: 10px; padding: 8px 12px; flex-wrap: nowrap; }
  .brand .sub { display: none; }
  .brand h1 { display: none; } /* mobile : on ne garde que le logo */
  .brand .logo { font-size: 26px; }
  .save-info { display: none; }
  .status { max-width: 26vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .actions { gap: 8px; margin-left: auto; flex-wrap: nowrap; }
  .btn { padding: 9px 12px; }
  /* Réinitialiser / Partager en icônes compactes */
  #btn-reset .btn-txt, #btn-share .btn-txt { display: none; }
  #btn-reset .btn-ico, #btn-share .btn-ico { display: inline; font-size: 17px; }
  .status { max-width: 18vw; }

  .main { position: relative; }
  .palette {
    position: fixed; top: var(--topbar-h, 56px); left: 0; bottom: 0;
    width: min(320px, 85vw); z-index: 50;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 2px 0 20px rgba(0, 0, 0, .22);
    -webkit-overflow-scrolling: touch;
  }
  body.drawer-open .palette { transform: translateX(0); }

  .drawer-backdrop {
    display: block; position: fixed;
    top: var(--topbar-h, 56px); left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, .38);
    opacity: 0; pointer-events: none; transition: opacity .25s ease; z-index: 40;
  }
  body.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

  /* La carte tient dans l'écran, sans marges horizontales */
  .canvas-wrap { padding: 12px 0; align-items: center; }
  #stage {
    width: auto; height: auto;
    max-width: 100%;
    max-height: calc(100dvh - var(--topbar-h, 56px) - 24px);
    box-shadow: 0 3px 14px rgba(0, 0, 0, .12);
  }

  /* Cibles tactiles plus larges */
  .palette-items { grid-template-columns: 1fr 1fr; gap: 10px; }
  .pal-item { padding: 10px 6px; }
}

@media (max-width: 400px) {
  .palette { width: 86vw; }
}
