:root {
  --bg: #f4f5f2;
  --panel: #ffffff;
  --ink: #1f2a1c;
  --muted: #6b7466;
  --line: #dfe3db;
  --accent: #2f6b3a;
  --accent-ink: #ffffff;
  --rack: #7f9d70;
  --rack-dark: #4d7a3a;
  --sol: #ccd8c6;
  --petit: #7d93a8;
  --hit: #f97316;
  --sel: #2563eb;
  --danger: #b91c1c;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { display: flex; flex-direction: column; font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
code { background: #eef0eb; padding: 1px 4px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- Barre du haut ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; white-space: nowrap; }
.search { position: relative; flex: 1 1 320px; max-width: 560px; }
.search input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg); outline: none;
}
.search input:focus { border-color: var(--accent); background: #fff; }
.search-results {
  position: absolute; z-index: 50; top: calc(100% + 4px); left: 0; right: 0; max-height: 60vh; overflow: auto;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 10px 30px rgba(0,0,0,.12);
}
.sr-group { padding: 6px 12px 2px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.sr-item { display: flex; gap: 8px; align-items: center; padding: 8px 12px; cursor: pointer; }
.sr-item:hover, .sr-item.active { background: #eef4ec; }
.sr-item .codes { margin-left: auto; font-weight: 600; color: var(--accent); text-align: right; }
.sr-empty { padding: 12px; color: var(--muted); }
.tabs { display: flex; gap: 2px; }
.tabs button {
  border: 0; background: transparent; padding: 8px 12px; border-radius: var(--radius); cursor: pointer; color: var(--muted); font-weight: 550;
}
.tabs button.active { background: #e6efe3; color: var(--accent); }
.save-status { font-size: 12px; color: var(--muted); min-width: 90px; text-align: right; }
.save-status.err { color: var(--danger); font-weight: 600; }
.who { display: flex; align-items: center; gap: 8px; font-size: 12px; white-space: nowrap; }
.who .role { padding: 2px 8px; border-radius: 99px; background: #eef0eb; color: var(--muted); font-weight: 600; }
.who .role.admin { background: #e6efe3; color: var(--accent); }
.linkbtn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; font: inherit; text-decoration: underline; }

/* Connexion */
.login { position: fixed; inset: 0; z-index: 250; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-box { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: 100%; max-width: 380px; display: grid; gap: 12px; box-shadow: 0 20px 50px rgba(0,0,0,.08); }
.login-box h1 { margin: 0; font-size: 20px; }
.login-box p { margin: 0; }
.login-box input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; }

/* Lecture seule : rien de modifiable n'est proposé */
body.readonly .admin-only,
body.readonly [data-act="assign-slot"], body.readonly [data-act="free-slot"], body.readonly .off-toggle,
body.readonly [data-act="site-new"], body.readonly [data-act="site-edit"], body.readonly [data-act="site-delete"] { display: none !important; }

main { flex: 1; min-height: 0; position: relative; }
.tab { display: none; height: 100%; }
.tab.active { display: block; }

/* ---------- Plan ---------- */
.plan-layout { display: flex; height: 100%; }
.plan-area { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
/* Légende des chantiers (couleurs) */
.site-legend {
  position: absolute; left: 12px; bottom: 12px; z-index: 5; min-width: 210px; max-width: 290px; max-height: 45%; overflow: auto;
  background: rgba(255,255,255,.96); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.12); font-size: 13px;
}
.site-legend button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; width: 100%; text-align: left; }
.lg-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; font-weight: 650; position: sticky; top: 0; background: inherit; }
.lg-list { padding-bottom: 4px; }
.lg-row { display: flex; align-items: center; gap: 8px; padding: 5px 10px; }
.lg-row:hover { background: #eef4ec; }
.lg-row.active { background: #fff7ed; font-weight: 650; }
.lg-row i { width: 14px; height: 14px; border-radius: 3px; flex: none; border: 1px solid rgba(0,0,0,.2); }
.lg-row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lg-row .cd { color: var(--muted); font-size: 11px; }
.lg-row .n { color: var(--muted); font-size: 12px; min-width: 18px; text-align: right; }
.plan-toolbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-bottom: 1px solid var(--line);
  background: var(--panel); flex-wrap: wrap;
}
.plan-select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.sw { width: 14px; height: 14px; border-radius: 3px; display: inline-block; border: 1px solid rgba(0,0,0,.25); }
.sw.rack { background: var(--rack); }
.sw.petit { background: var(--petit); }
.sw.sol { background: var(--sol); }
.sw.occ { background: linear-gradient(135deg, #c2410c 50%, #2563eb 50%); }
.view-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.view-toggle button { border: 0; background: #fff; padding: 6px 14px; cursor: pointer; font-weight: 600; color: var(--muted); }
.view-toggle button + button { border-left: 1px solid var(--line); }
.view-toggle button.active { background: var(--accent); color: #fff; }
.viewer3d { cursor: default; }
.viewer3d canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.tip3d { position: absolute; pointer-events: none; background: #1f2a1c; color: #fff; padding: 5px 9px; border-radius: 6px; font-size: 13px; white-space: nowrap; box-shadow: 0 4px 14px rgba(0,0,0,.25); }
.tip3d span { opacity: .75; }
.zoom-btns { margin-left: auto; display: flex; gap: 4px; }
.zoom-btns button {
  width: 32px; height: 32px; border: 1px solid var(--line); background: #fff; border-radius: 6px; cursor: pointer; font-size: 16px;
}
.viewer { flex: 1; position: relative; overflow: hidden; background: #fff; touch-action: none; cursor: grab; }
.viewer.panning { cursor: grabbing; }
.viewer.drawing { cursor: crosshair; }
.viewer svg { width: 100%; height: 100%; display: block; user-select: none; }
.move-banner {
  background: #fff7ed; color: #9a3412; padding: 8px 12px; border-bottom: 1px solid #fed7aa; display: flex; gap: 12px; align-items: center;
}

/* Cellules SVG */
.cell { cursor: pointer; }
.cell .bg { stroke: #1f2a1c; stroke-width: 1.5; }
.cell.rack .bg { fill: var(--rack); }
.cell.petit .bg { fill: var(--petit); }
.cell.sol .bg { fill: var(--sol); }
.cell .lvl { fill: rgba(255,255,255,.55); stroke: rgba(0,0,0,.25); stroke-width: 1; }
.cell .lbl { font-size: 17px; font-weight: 700; fill: #1f2a1c; pointer-events: none; text-anchor: middle; dominant-baseline: middle; }
.cell .lbl.on-dark { fill: #fff; paint-order: stroke; stroke: rgba(0,0,0,.45); stroke-width: 3px; }
.cell:hover .bg { stroke: var(--sel); }
.cell .ring { fill: none; pointer-events: none; }
/* Emplacements désactivés */
.cell.off .bg { fill: #d7d9d4 !important; stroke-dasharray: 6 4; }
.cell.off .lbl { fill: #8a9187; stroke: none; }
.cell .lvl.off { fill: #8a9187; opacity: .55; }
.level.off { background: #f3f4f2; }
.off-toggle { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); white-space: nowrap; cursor: pointer; margin-left: 4px; }
.off-toggle input { margin: 0; }
.cell .ring.sel { stroke: var(--sel); }
.cell .ring.hit { stroke: var(--hit); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { stroke-opacity: .35; } }
.plan-label { font-family: Helvetica, Arial, sans-serif; fill: #1f2a1c; text-anchor: middle; dominant-baseline: central; pointer-events: none; user-select: none; }
.edit .plan-label { pointer-events: all; cursor: move; }
.edit .plan-label:hover, .edit .plan-label.selected { fill: var(--sel) !important; text-decoration: underline; }
.zone-frame { fill: none; stroke: transparent; stroke-width: 2; }
.zone-label { font-size: 20px; fill: #334; pointer-events: none; font-weight: 600; paint-order: stroke; stroke: #fff; stroke-width: 4px; }

/* Édition */
.edit .zone { cursor: move; }
.edit .zone .zone-frame { stroke: rgba(37,99,235,.35); stroke-dasharray: 6 4; }
.edit .zone.selected .zone-frame { stroke: var(--sel); stroke-width: 4; stroke-dasharray: none; }
.handle { fill: #fff; stroke: var(--sel); stroke-width: 3; }
.handle.nw, .handle.se { cursor: nwse-resize; }
.handle.ne, .handle.sw { cursor: nesw-resize; }
.draw-rect { fill: rgba(37,99,235,.12); stroke: var(--sel); stroke-width: 3; stroke-dasharray: 8 4; }

/* ---------- Panneau latéral ---------- */
.side {
  width: 360px; flex: none; background: var(--panel); border-left: 1px solid var(--line); overflow: auto; padding: 14px;
}
.side h2 { font-size: 16px; margin: 0 0 4px; }
.side h3 { font-size: 13px; margin: 18px 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.side .sub { color: var(--muted); margin-bottom: 10px; }
.level {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center;
}
.level .code { font-weight: 700; min-width: 46px; font-size: 15px; }
.level .body { flex: 1; min-width: 0; }
.level .body .t { font-weight: 600; }
.level .body .d { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.level.empty { border-style: dashed; color: var(--muted); }
.level .acts { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 1px 8px; border-radius: 99px; background: #eef0eb; white-space: nowrap; }
.chip i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.pal-item {
  display: flex; gap: 8px; align-items: center; padding: 7px 8px; border: 1px solid var(--line); border-radius: 6px; margin-bottom: 6px;
  background: #fff; cursor: grab;
}
.pal-item:hover { border-color: var(--accent); }
.pal-item .n { font-weight: 650; }
.pal-item .d { color: var(--muted); font-size: 12px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-box { background: #fff7ed; border: 1px solid #fed7aa; border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; }
.result-box .big { font-size: 18px; font-weight: 700; color: #9a3412; }
.result-box .codes { font-size: 16px; font-weight: 700; margin: 4px 0; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.stat { background: var(--bg); border-radius: var(--radius); padding: 8px; text-align: center; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 11px; color: var(--muted); }

/* ---------- Boutons et formulaires ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border: 1px solid var(--line); background: #fff;
  border-radius: 6px; cursor: pointer; font-weight: 550; white-space: nowrap;
}
.btn:hover { border-color: #b9c2b3; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.btn.active { background: #dbeafe; border-color: var(--sel); color: var(--sel); }
.form { display: grid; gap: 10px; }
.form label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 550; }
.form input, .form select, .form textarea {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); font-size: 14px; width: 100%;
}
.form textarea { min-height: 70px; resize: vertical; }
.form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form .row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.form .check { display: flex; align-items: center; gap: 8px; color: var(--ink); font-weight: 400; font-size: 14px; }
.form .check input { width: auto; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.warn { background: #fef2f2; color: var(--danger); border-radius: 6px; padding: 8px 10px; font-size: 13px; }
.preview-codes { font-size: 12px; color: var(--muted); background: var(--bg); border-radius: 6px; padding: 6px 8px; line-height: 1.6; }

/* ---------- Pages ---------- */
.page { max-width: 1200px; margin: 0 auto; padding: 20px 16px 40px; height: 100%; overflow: auto; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { margin: 0; font-size: 22px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input, .filters select { padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.filters input { flex: 1 1 260px; }
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbf9; cursor: pointer; white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #f6f9f5; }
.table .loc { font-weight: 700; color: var(--accent); }
.table .none { color: var(--muted); font-style: italic; }
.site-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.site-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; border-top: 5px solid var(--c, #999); }
.site-card h3 { margin: 0 0 2px; font-size: 17px; }
.site-card .codes { font-weight: 700; color: var(--accent); margin: 8px 0; word-break: break-word; }

/* ---------- Modale et toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(20,25,18,.45); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-box { background: #fff; border-radius: 12px; width: 100%; max-width: 520px; max-height: 90vh; overflow: auto; padding: 20px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-box h2 { margin: 0 0 14px; font-size: 18px; }
.pick-list { max-height: 320px; overflow: auto; margin-top: 8px; }
.pick-list .pal-item { cursor: pointer; }
.history { font-size: 12px; color: var(--muted); max-height: 140px; overflow: auto; margin: 0; padding-left: 18px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1f2a1c; color: #fff; padding: 10px 16px;
  border-radius: 8px; z-index: 200; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: calc(100% - 32px);
}
.toast.err { background: var(--danger); }

@media (max-width: 860px) {
  .plan-layout { flex-direction: column; }
  .side { width: 100%; border-left: 0; border-top: 1px solid var(--line); max-height: 45%; }
  .legend { display: none; }
  .topbar { gap: 8px; }
}
@media print {
  .topbar, .plan-toolbar, .side, .filters, .actions { display: none !important; }
}
