/* OptiChart — editor chrome.
   Diagram content is styled inline from the document's own `style` objects, so
   that exported SVG carries its appearance with it.

   Colours, radii, fonts and the generic components (buttons, menus, form
   controls, modals, toasts) come from the toolbox: /_toolbox/ui/tokens.css
   and /_toolbox/ui/components.css, linked before this file. */

:root {
  --grid-dot: #c8d2e0;
  --selection: #3b6fb0;
  --guide: #e5528a;

  --toolbar-h: 52px;
  --panel-w: 268px;
  --palette-w: 132px;
  --library-w: 256px;
}

:root[data-theme="dark"] {
  --grid-dot: #2f3b4a;
  --selection: #5b9bd8;
  --guide: #f06ba0;
}

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

body {
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

#i-select path {
  fill: currentColor;
  stroke-linejoin: round;
}

/* ---------------------------------------------------------------- layout -- */

#app {
  display: grid;
  grid-template-columns: var(--library-w) var(--palette-w) 1fr var(--panel-w);
  grid-template-rows: var(--toolbar-h) 1fr;
  grid-template-areas:
    "toolbar toolbar toolbar   toolbar"
    "library palette stage     inspector";
  height: 100%;
  transition: grid-template-columns 0.16s ease;
}

/* Masquer le volet : la colonne se referme, le contenu ne se réorganise pas. */
#app.library-hidden {
  grid-template-columns: 0 var(--palette-w) 1fr var(--panel-w);
}

#app.library-hidden #library {
  border-right-width: 0;
  overflow: hidden;
}

#toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}

#library {
  grid-area: library;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

#palette {
  grid-area: palette;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px 8px 20px;
}

#stage {
  grid-area: stage;
  position: relative;
  overflow: hidden;
  background: var(--surface-sunken);
}

#inspector {
  grid-area: inspector;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px 12px 40px;
}

.panel-title {
  margin: 2px 0 10px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
}

/* --------------------------------------------------------------- toolbar -- */

.tb-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.tb-group + .tb-group::before {
  content: "";
  width: 1px;
  height: 22px;
  margin-right: 10px;
  background: var(--border);
}

.tb-identity {
  gap: 8px;
  min-width: 0;
}

.tb-right {
  margin-left: auto;
}

.brand-mark {
  display: block;
  width: 22px;
  height: 22px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
}

.brand-mark .brand-link {
  stroke: var(--text-faint);
}

.doc-name {
  width: 210px;
  min-width: 90px;
  padding: 5px 8px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
}

.doc-name:hover {
  border-color: var(--border);
}

.doc-name:focus {
  outline: none;
  background: var(--surface-2);
  border-color: var(--accent);
}

.save-state {
  font-size: 11px;
  color: var(--text-faint);
  white-space: nowrap;
}

.save-state[data-state="saving"] {
  color: var(--warn);
}

.save-state[data-state="saved"] {
  color: var(--ok);
}

.save-state[data-state="error"],
.save-state[data-state="conflict"] {
  color: var(--danger);
  font-weight: 600;
}

/* The way back to the toolbox portal is a link dressed as the icon buttons. */
a.icon-btn {
  color: inherit;
  text-decoration: none;
}

/* Styled like .icon-btn and .btn (packages/ui/components.css). */
.zoom-level {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 30px;
  padding: 0 9px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}

.zoom-level {
  min-width: 52px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

.zoom-level:hover {
  background: var(--surface-sunken);
  border-color: var(--border-strong);
}

/* --------------------------------------------------------------- palette -- */

.shape-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 5px;
}

.shape-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 7px 2px 5px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: grab;
  color: var(--text-dim);
}

.shape-btn:hover {
  background: var(--surface-sunken);
  border-color: var(--border);
}

.shape-btn.is-active {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent);
}

.shape-btn svg {
  width: 36px;
  height: 27px;
  overflow: visible;
}

/* Les noms français sont plus longs que la case : on tronque, l'infobulle du
   bouton donne le nom complet. */
.shape-btn span {
  max-width: 100%;
  overflow: hidden;
  font-size: 9.5px;
  letter-spacing: 0.01em;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.palette-hint {
  margin: 14px 2px 0;
  font-size: 10.5px;
  line-height: 1.45;
  color: var(--text-faint);
}

/* ---------------------------------------------------------------- canvas -- */

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
}

#canvas:focus {
  outline: none;
}

#canvas.tool-pan,
#canvas.is-panning {
  cursor: grab;
}

#canvas.is-panning {
  cursor: grabbing;
}

#canvas.tool-connect,
#canvas.tool-shape {
  cursor: crosshair;
}

.grid-dot {
  fill: var(--grid-dot);
}

#canvas-grid.is-hidden {
  display: none;
}

.node {
  cursor: move;
}

.node-shape {
  vector-effect: non-scaling-stroke;
}

.node-text,
.edge-label-text {
  pointer-events: none;
  white-space: pre;
}

.edge-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 14;
  cursor: pointer;
}

.edge-line {
  fill: none;
  stroke-linejoin: round;
  stroke-linecap: round;
  pointer-events: none;
}

.is-editing .node-text,
.is-editing .edge-label-text {
  visibility: hidden;
}

/* Editor chrome — the whole overlay layer is dropped on SVG export. */
.overlay {
  pointer-events: none;
}

.overlay .hit {
  pointer-events: all;
}

.sel-outline {
  fill: none;
  stroke: var(--selection);
  stroke-opacity: 0.9;
}

.sel-handle {
  fill: var(--surface);
  stroke: var(--selection);
}

.sel-handle.nw,
.sel-handle.se {
  cursor: nwse-resize;
}

.sel-handle.ne,
.sel-handle.sw {
  cursor: nesw-resize;
}

.sel-handle.n,
.sel-handle.s {
  cursor: ns-resize;
}

.sel-handle.e,
.sel-handle.w {
  cursor: ew-resize;
}

.port {
  fill: var(--surface);
  stroke: var(--accent);
  cursor: crosshair;
}

.port:hover,
.port.is-hot {
  fill: var(--accent);
}

.port-target {
  fill: transparent;
  cursor: crosshair;
}

.waypoint {
  fill: var(--surface);
  stroke: var(--selection);
  cursor: move;
}

.waypoint.is-ghost {
  fill: var(--selection);
  fill-opacity: 0.35;
  stroke: none;
  cursor: copy;
}

.endpoint {
  fill: var(--surface);
  stroke: var(--selection);
  cursor: move;
}

.marquee {
  fill: var(--selection);
  fill-opacity: 0.08;
  stroke: var(--selection);
  stroke-opacity: 0.75;
}

.guide {
  stroke: var(--guide);
  stroke-opacity: 0.95;
}

.drop-target > .node-shape {
  stroke: var(--selection) !important;
  stroke-opacity: 1;
}

.link-preview {
  fill: none;
  stroke: var(--selection);
  stroke-linecap: round;
}

/* Inline text editing overlay, positioned over the shape being edited. */
.text-editor {
  position: absolute;
  z-index: 4;
  overflow: hidden;
  padding: 0;
  margin: 0;
  line-height: 1.25;
  background: transparent;
  border: 0;
  outline: 2px solid var(--selection);
  outline-offset: 3px;
  border-radius: 3px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  transform-origin: 0 0;
}

.empty-hint {
  position: absolute;
  inset: auto 0 42%;
  text-align: center;
  color: var(--text-faint);
  pointer-events: none;
}

/* ------------------------------------------------------------- inspector -- */

.insp-section + .insp-section {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.insp-head {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-dim);
}

.field {
  display: grid;
  grid-template-columns: 74px 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 7px;
}

.field > label {
  font-size: 11px;
  color: var(--text-dim);
}

.field-wide {
  grid-template-columns: 1fr;
  gap: 4px;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 7px;
}

.field-row .field {
  grid-template-columns: 22px 1fr;
  margin-bottom: 0;
}

input[type="color"] {
  width: 100%;
  height: 26px;
  padding: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.color-field {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px;
  align-items: center;
}

.color-none {
  padding: 0 6px;
  height: 26px;
  font-size: 10px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.color-none.is-active {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.swatches {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin: 6px 0 2px;
}

.swatch {
  aspect-ratio: 1;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  cursor: pointer;
}

.btn-row {
  display: grid;
  grid-auto-flow: column;
  gap: 4px;
  margin-bottom: 6px;
}

.btn-row .icon-btn,
.btn-row .btn {
  width: 100%;
  border-color: var(--border);
  background: var(--surface-2);
}

.align-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 3px;
}

.align-grid button {
  height: 26px;
  padding: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

.align-grid svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: var(--text-dim);
  stroke-width: 1.6;
  stroke-linecap: round;
}

.align-grid button:hover svg {
  stroke: var(--accent);
}

.data-table {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 4px;
  align-items: center;
}

.data-table .icon-btn {
  width: 24px;
  height: 24px;
  color: var(--text-faint);
}

.data-table .icon-btn:hover {
  color: var(--danger);
}

.data-table .icon {
  width: 14px;
  height: 14px;
}

.insp-empty {
  padding: 6px 2px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-faint);
}

.insp-note {
  margin: 6px 0 0;
  font-size: 10.5px;
  color: var(--text-faint);
}

/* ------------------------------------------------------- volet bibliothèque */

.lib-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
  min-height: 44px;
  padding: 10px 8px 4px 12px;
}

.lib-crumbs {
  display: grid;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.lib-crumbs .panel-title {
  margin: 6px 0 0;
}

.lib-title-row {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  margin-left: -6px;
}

.lib-back {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  color: var(--text-dim);
}

.lib-folder-title {
  overflow: hidden;
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.lib-ancestors {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1px 2px;
  padding-left: 22px;
  font-size: 11px;
  color: var(--text-faint);
}

.lib-crumb {
  max-width: 110px;
  overflow: hidden;
  padding: 1px 4px;
  font: inherit;
  color: var(--text-dim);
  white-space: nowrap;
  text-overflow: ellipsis;
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
}

.lib-crumb:hover {
  color: var(--accent);
  background: var(--surface-sunken);
}

.lib-sep {
  color: var(--text-faint);
}

.lib-head-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 2px;
}

.lib-head-actions .icon-btn {
  width: 26px;
  height: 26px;
  color: var(--text-dim);
}

.lib-search {
  position: relative;
  margin: 4px 10px 6px;
}

#library-search {
  width: 100%;
  padding-right: 28px;
}

#library-search::-webkit-search-cancel-button {
  display: none;
}

#library-search-clear {
  position: absolute;
  top: 50%;
  right: 3px;
  width: 22px;
  height: 22px;
  color: var(--text-faint);
  transform: translateY(-50%);
}

#library-search-clear[hidden] {
  display: none;
}

#library-search-clear .icon {
  width: 13px;
  height: 13px;
}

.lib-filter {
  display: flex;
  gap: 2px;
  margin: 0 10px 8px;
  padding: 2px;
  background: var(--surface-sunken);
  border-radius: var(--radius-sm);
}

.lib-filter button {
  display: inline-flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 24px;
  padding: 0 4px;
  font: inherit;
  font-size: 11px;
  font-weight: 550;
  color: var(--text-dim);
  background: none;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
}

.lib-filter button .icon {
  width: 12px;
  height: 12px;
}

.lib-filter button:hover {
  color: var(--text);
}

.lib-filter button[aria-checked="true"] {
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.library-tree {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 20px;
}

.lib-section {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  padding: 4px 6px;
  font: inherit;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
  background: none;
  border: 0;
  cursor: pointer;
}

.lib-section:hover {
  color: var(--text-dim);
}

.lib-twisty {
  width: 12px;
  height: 12px;
  transition: transform 0.14s ease;
}

.lib-section.is-open .lib-twisty {
  transform: rotate(90deg);
}

.lib-divider {
  height: 1px;
  margin: 6px 6px 6px;
  background: var(--border);
}

.lib-row {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr) auto;
  gap: 7px;
  align-items: center;
  min-height: 30px;
  padding: 4px 6px 4px 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.lib-row:hover {
  background: var(--surface-sunken);
}

.lib-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.lib-row.is-current {
  background: var(--accent-soft);
}

.lib-row.is-current .lib-name {
  color: var(--accent);
  font-weight: 600;
}

.lib-row.is-dragging {
  opacity: 0.45;
}

.is-drop-target,
.lib-row.is-drop-target {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.lib-icon {
  width: 15px;
  height: 15px;
  color: var(--text-faint);
}

.folder-icon {
  color: var(--accent);
}

.lib-label {
  display: grid;
  min-width: 0;
}

.lib-name {
  overflow: hidden;
  font-size: 12.5px;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.folder-row .lib-name {
  font-weight: 550;
}

.lib-path {
  overflow: hidden;
  font-size: 10.5px;
  color: var(--text-faint);
  white-space: nowrap;
  text-overflow: ellipsis;
}

.lib-tail {
  display: flex;
  align-items: center;
  gap: 2px;
}

.folder-count {
  font-size: 10.5px;
  font-variant-numeric: tabular-nums;
  color: var(--text-faint);
}

.lib-go {
  width: 13px;
  height: 13px;
  color: var(--text-faint);
}

.lib-visibility {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  color: var(--text-faint);
}

.lib-visibility .icon {
  width: 13px;
  height: 13px;
}

.lib-visibility.is-private {
  color: var(--warn);
}

/* Les actions prennent la place de la queue de ligne au survol. */
.row-actions {
  display: none;
  gap: 0;
}

.lib-row {
  grid-template-areas: "icon label tail";
}

.lib-row > .lib-icon {
  grid-area: icon;
}

.lib-row > .lib-label {
  grid-area: label;
}

.lib-row > .lib-tail,
.lib-row > .row-actions {
  grid-area: tail;
}

@media (hover: hover) {
  .lib-row:hover > .row-actions,
  .lib-row:focus-within > .row-actions {
    display: flex;
  }

  .lib-row:hover > .lib-tail,
  .lib-row:focus-within > .lib-tail {
    visibility: hidden;
  }
}

.row-actions .icon-btn {
  width: 22px;
  height: 22px;
  color: var(--text-faint);
}

.row-actions .icon-btn:hover {
  color: var(--accent);
  background: transparent;
  border-color: transparent;
}

.row-actions .icon {
  width: 13.5px;
  height: 13.5px;
}

/* Le bouton de suppression est rouge en permanence : pas de confirmation,
   l'intention doit donc être lisible avant le clic. */
.row-actions .icon-btn.is-danger {
  color: var(--danger);
  opacity: 0.75;
}

.row-actions .icon-btn.is-danger:hover {
  color: var(--danger);
  opacity: 1;
}

.library-empty {
  padding: 22px 12px;
  font-size: 11.5px;
  line-height: 1.5;
  text-align: center;
  color: var(--text-faint);
}

.rename-input {
  width: 100%;
  padding: 2px 5px;
  font: inherit;
  font-size: 12px;
}

/* -------------------------------------------------- « Déplacer vers… » -- */

.modal-small {
  width: min(380px, 92vw);
}

.move-list {
  display: grid;
  gap: 1px;
  padding: 8px;
  overflow-y: auto;
}

.move-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 4px 10px;
  font: inherit;
  font-size: 13px;
  text-align: left;
  color: var(--text);
  background: none;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.move-option .icon {
  width: 15px;
  height: 15px;
  color: var(--accent);
}

.move-option:hover:not(:disabled),
.move-option:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.move-option:disabled {
  color: var(--text-faint);
  cursor: default;
}

.move-option:disabled .icon {
  color: var(--text-faint);
}

.move-note {
  margin-left: auto;
  font-size: 11px;
}

/* ------------------------------------------------------ menu contextuel -- */

.ctx-layer {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
}

.ctx-menu {
  position: fixed;
  display: flex;
  flex-direction: column;
  min-width: 200px;
  max-width: 280px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  pointer-events: auto;
  animation: ctx-in 0.08s ease-out;
}

@keyframes ctx-in {
  from {
    opacity: 0;
    transform: scale(0.98);
  }
}

.ctx-title {
  overflow: hidden;
  padding: 6px 10px 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
  text-overflow: ellipsis;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.ctx-item {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 9px;
  align-items: center;
  min-height: 30px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  text-align: left;
  color: var(--text);
  background: none;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.ctx-item .icon {
  width: 15px;
  height: 15px;
  color: var(--text-dim);
}

.ctx-item:hover:not(:disabled),
.ctx-item:focus-visible {
  background: var(--surface-sunken);
  outline: none;
}

.ctx-item.is-danger,
.ctx-item.is-danger .icon {
  color: var(--danger);
}

.ctx-item.is-danger:hover,
.ctx-item.is-danger:focus-visible {
  background: var(--danger-soft);
}

.ctx-item:disabled {
  opacity: 0.45;
  cursor: default;
}

.ctx-sep {
  height: 1px;
  margin: 4px 2px;
  background: var(--border);
}

/* Sur téléphone, le menu devient une feuille d'actions en bas d'écran. */
.ctx-layer.is-sheet {
  pointer-events: auto;
}

.ctx-backdrop {
  position: absolute;
  inset: 0;
  background: rgb(11 18 26 / 40%);
  animation: fade-in 0.15s ease-out;
}

.ctx-layer.is-sheet .ctx-menu {
  inset: auto 0 0;
  max-width: none;
  padding: 8px 8px calc(10px + env(safe-area-inset-bottom));
  border-radius: 16px 16px 0 0;
  animation: sheet-up 0.18s ease-out;
}

.ctx-layer.is-sheet .ctx-item {
  min-height: 46px;
  font-size: 15px;
}

.ctx-layer.is-sheet .ctx-title {
  font-size: 13px;
  text-align: center;
}

@keyframes sheet-up {
  from {
    transform: translateY(100%);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.help-body {
  overflow-y: auto;
  padding: 6px 16px 18px;
}

.help-body h3 {
  margin: 14px 0 6px;
  font-size: 11px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.help-row {
  display: grid;
  grid-template-columns: 128px 1fr;
  gap: 10px;
  padding: 3px 0;
}

kbd {
  display: inline-block;
  padding: 1px 5px;
  font-family: var(--mono);
  font-size: 11px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
}

/* Écrans étroits : les deux volets latéraux passent au-dessus du canevas. */
@media (max-width: 1100px) {
  #app,
  #app.library-hidden {
    grid-template-columns: var(--palette-w) 1fr;
    grid-template-areas:
      "toolbar toolbar"
      "palette stage";
  }

  #library {
    position: fixed;
    top: var(--toolbar-h);
    bottom: 0;
    left: 0;
    width: var(--library-w);
    z-index: 6;
    box-shadow: var(--shadow);
  }

  #app.library-hidden #library {
    display: none;
  }

  #inspector {
    position: fixed;
    top: var(--toolbar-h);
    right: 0;
    bottom: 0;
    width: var(--panel-w);
    z-index: 6;
    box-shadow: var(--shadow);
  }
}

/* ------------------------------------------- boutons à icône + libellé -- */

.btn-labelled {
  gap: 6px;
  padding: 0 10px 0 9px;
}

.btn-labelled > .icon {
  width: 16px;
  height: 16px;
  color: var(--text-dim);
}

.btn-labelled:hover > .icon {
  color: var(--accent);
}

.btn-labelled .btn-caret {
  width: 12px;
  height: 12px;
  margin-left: -2px;
  color: var(--text-faint);
}

/* En dessous de 1300 px, les libellés cèdent la place : icônes seules, avec
   l'infobulle et l'aria-label pour dire ce qu'elles font. */
@media (max-width: 1300px) {
  .btn-labelled {
    padding: 0 7px;
  }

  .btn-labelled .btn-label,
  #current-user {
    display: none;
  }

  #btn-user {
    padding: 0 5px;
  }
}

/* ------------------------------------------------------------ compte -- */

#btn-user {
  gap: 6px;
  max-width: 180px;
}

#current-user {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#user-menu {
  min-width: 230px;
}

/* ─────────────────────────────────── Visibility Control ─────────────────────────────────── */

.owner-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
  background: var(--bg2);
  border-radius: 8px;
  transition: all 0.2s ease;
}

.owner-control[style*="cursor: not-allowed"] {
  opacity: 0.5;
}

.visibility-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--fg2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: color 0.3s ease;
  flex: 0 0 auto;
  min-width: 45px;
  text-align: center;
}

.visibility-label-left {
  text-align: right;
}

.visibility-label-right {
  text-align: left;
}

.owner-control:not([style*="cursor: not-allowed"]) .visibility-label-left:hover {
  color: var(--fg1);
}

.owner-control:not([style*="cursor: not-allowed"]) .visibility-label-right:hover {
  color: var(--fg1);
}

.owner-toggle {
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
  position: relative;
  flex: 0 0 auto;
}

.owner-toggle input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.owner-slider {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 50px;
  height: 26px;
  background: #d0d5dd;
  border: none;
  border-radius: 13px;
  transition: background 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06);
}

.owner-slider::after {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  left: 2px;
  transition: all 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.owner-toggle input[type="checkbox"]:checked + .owner-slider {
  background: #3b6fb0;
}

.owner-toggle input[type="checkbox"]:checked + .owner-slider::after {
  left: 26px;
}

.owner-toggle input[type="checkbox"]:disabled + .owner-slider {
  cursor: not-allowed;
}

.owner-toggle input[type="checkbox"]:not(:disabled):hover + .owner-slider {
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.06), 0 0 0 2px rgba(59, 111, 176, 0.1);
}

.owner-toggle input[type="checkbox"]:not(:disabled):active + .owner-slider::after {
  width: 26px;
}

/* ─────────────────────────────────── History modal ─────────────────────────────────── */

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0;
  max-height: 60vh;
  overflow-y: auto;
  padding: 0;
}

.history-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.history-item:last-child {
  border-bottom: none;
}

.history-item-info {
  flex: 1;
  min-width: 0;
}

.history-item-time {
  font-size: 12px;
  color: var(--fg2);
  margin-bottom: 4px;
}

.history-item-description {
  font-size: 13px;
  color: var(--fg1);
  word-break: break-word;
}

.history-item-origin {
  font-size: 11px;
  color: var(--fg3);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.history-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.history-item-actions button {
  padding: 6px 10px;
  font-size: 12px;
  background: var(--bg2);
  color: var(--fg1);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.history-item-actions button:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.history-item-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.history-empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--fg2);
  font-size: 13px;
}

/* ------------------------------------------------------------- téléphone -- */

/* Ce qui n'existe que sur téléphone reste invisible sur un écran plus large. */
@media (min-width: 701px) {
  .phone-only {
    display: none !important;
  }
}

@media (max-width: 700px) {
  :root {
    --mobile-bar-h: calc(58px + env(safe-area-inset-bottom));
  }

  html,
  body {
    overscroll-behavior: none;
  }

  body {
    touch-action: manipulation;
  }

  #app,
  #app.library-hidden {
    grid-template-columns: 1fr;
    grid-template-rows: calc(var(--toolbar-h) + env(safe-area-inset-top)) 1fr var(--mobile-bar-h);
    grid-template-areas:
      "toolbar"
      "stage"
      "bar";
  }

  /* ---- barre du haut : bibliothèque, nom, état, annuler/rétablir, ⋯, compte */

  #toolbar {
    gap: 2px;
    padding: env(safe-area-inset-top) calc(6px + env(safe-area-inset-right)) 0 calc(6px + env(safe-area-inset-left));
  }

  .tb-group + .tb-group::before {
    display: none;
  }

  .brand,
  #btn-copy-link,
  #toolbar .tb-group[role="radiogroup"],
  #btn-zoom-out,
  #btn-zoom-level,
  #btn-zoom-in,
  #btn-fit,
  #btn-history,
  #btn-export,
  #btn-import,
  #btn-theme,
  #btn-help {
    display: none;
  }

  .tb-identity {
    flex: 1;
    gap: 2px;
  }

  .doc-name {
    flex: 1;
    width: auto;
    min-width: 0;
    font-size: 15px;
  }

  /* L'état d'enregistrement devient une pastille ; le texte reste pour les
     lecteurs d'écran. */
  .save-state {
    flex: 0 0 auto;
    width: 8px;
    height: 8px;
    overflow: hidden;
    font-size: 0;
    background: var(--text-faint);
    border-radius: 50%;
  }

  .save-state[data-state="saving"] {
    background: var(--warn);
  }

  .save-state[data-state="saved"] {
    background: var(--ok);
  }

  .save-state[data-state="error"],
  .save-state[data-state="conflict"] {
    background: var(--danger);
  }

  .tb-right {
    gap: 0;
    margin-left: 0;
  }

  #toolbar .icon-btn {
    width: 38px;
    height: 38px;
  }

  #btn-user {
    height: 38px;
    padding: 0 6px;
    border-color: transparent;
    background: none;
  }

  .user-avatar {
    width: 26px;
    height: 26px;
    font-size: 11px;
  }

  /* ---- bibliothèque en tiroir */

  #library,
  #app.library-hidden #library {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    display: flex;
    width: min(86vw, 340px);
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    transition: transform 0.22s ease;
  }

  #app.library-hidden #library {
    transform: translateX(-105%);
    box-shadow: none;
  }

  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgb(11 18 26 / 40%);
    animation: fade-in 0.18s ease-out;
  }

  .drawer-backdrop[hidden] {
    display: none;
  }

  .lib-row {
    min-height: 44px;
  }

  .lib-name {
    font-size: 14px;
  }

  .lib-filter button {
    height: 32px;
    font-size: 12.5px;
  }

  #library-search {
    height: 38px;
    font-size: 16px; /* moins de 16 px et iOS zoome sur le champ */
  }

  .lib-head-actions .icon-btn,
  .lib-back {
    width: 36px;
    height: 36px;
  }

  /* ---- formes et propriétés en feuilles du bas */

  #palette,
  #inspector {
    position: fixed;
    top: auto;
    right: 0;
    bottom: var(--mobile-bar-h);
    left: 0;
    z-index: 38;
    width: auto;
    max-height: 62vh;
    overflow-y: auto;
    padding: 0 calc(14px + env(safe-area-inset-right)) 18px calc(14px + env(safe-area-inset-left));
    background: var(--surface);
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 30px rgb(11 18 26 / 18%);
    transform: translateY(calc(100% + var(--mobile-bar-h)));
    transition: transform 0.22s ease;
  }

  #app.palette-open #palette,
  #app.inspector-open #inspector {
    transform: none;
  }

  .sheet-handle {
    width: 38px;
    height: 4px;
    margin: 8px auto 10px;
    background: var(--border-strong);
    border-radius: 2px;
  }

  #palette .shape-list {
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
    gap: 8px;
  }

  .palette-hint {
    display: none;
  }

  #inspector .field {
    grid-template-columns: 96px 1fr;
  }

  #inspector input,
  #inspector select,
  #inspector textarea {
    font-size: 16px;
  }

  /* ---- barre du bas */

  .mobile-bar {
    grid-area: bar;
    z-index: 39;
    display: flex;
    align-items: flex-start;
    justify-content: space-around;
    padding: 5px calc(6px + env(safe-area-inset-right)) env(safe-area-inset-bottom) calc(6px + env(safe-area-inset-left));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }

  .mobile-btn {
    position: relative;
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    max-width: 84px;
    padding: 5px 4px 4px;
    font-size: 10.5px;
    font-weight: 550;
    color: var(--text-dim);
    background: none;
    border: 0;
    border-radius: 12px;
    cursor: pointer;
  }

  .mobile-btn .icon {
    width: 22px;
    height: 22px;
  }

  .mobile-btn.is-active,
  .mobile-btn[aria-expanded="true"] {
    color: var(--accent);
    background: var(--accent-soft);
  }

  /* Une sélection a des propriétés à régler : une pastille le signale. */
  #app.has-selection #btn-props::after {
    content: "";
    position: absolute;
    top: 5px;
    right: calc(50% - 16px);
    width: 7px;
    height: 7px;
    background: var(--accent);
    border: 2px solid var(--surface);
    border-radius: 50%;
  }

  /* ---- divers */

  .toast {
    bottom: calc(var(--mobile-bar-h) + 12px);
    max-width: calc(100vw - 24px);
  }

  .modal {
    place-items: end center;
  }

  .modal-panel,
  .modal-narrow,
  .modal-small {
    width: 100vw;
    max-height: 88vh;
    padding-bottom: env(safe-area-inset-bottom);
    border-radius: 16px 16px 0 0;
  }

  #user-menu {
    position: fixed;
    top: calc(var(--toolbar-h) + env(safe-area-inset-top));
    right: 8px;
  }
}
