:root {
  --bg: #f4f7ef;
  --bg-ink: #1d2b1f;
  --panel: rgba(255, 255, 255, 0.76);
  --panel-border: #b9ccb4;
  --accent: #2f6a3f;
  --accent-soft: #9fcf84;
  --warm: #d6672b;
  --shadow: 0 18px 35px rgba(28, 47, 30, 0.16);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", sans-serif;
  color: var(--bg-ink);
  background: radial-gradient(circle at 10% 20%, #e7f5d9 0%, transparent 30%),
    radial-gradient(circle at 92% 12%, #ffe6d4 0%, transparent 25%),
    linear-gradient(135deg, #f8fbf4 0%, #edf4e4 100%);
  padding: 26px;
}

.bg-shape {
  position: fixed;
  z-index: -1;
  border-radius: 999px;
  filter: blur(18px);
  opacity: 0.5;
}

.bg-shape-a {
  width: 260px;
  height: 260px;
  background: #b7e1ad;
  top: -70px;
  left: -40px;
}

.bg-shape-b {
  width: 220px;
  height: 220px;
  background: #ffd4b6;
  right: 20px;
  bottom: -80px;
}

header {
  position: relative;
  margin: 0 auto 34px;
  max-width: 900px;
  animation: reveal 0.6s ease;
}

.header-logo {
  position: absolute;
  top: 0;
  right: 0;
  width: 110px;
  height: 110px;
  object-fit: contain;
}

.eyebrow {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--warm);
  font-size: 0.72rem;
  font-weight: 700;
}

h1 {
  margin: 26px 0 8px;
  font-family: "DM Serif Display", serif;
  font-size: clamp(1.8rem, 2.8vw, 2.2rem);
  font-weight: 400;
}

.logo-1 {
  font-family: 'Tangerine', cursive;
  font-weight: 700;
  font-size: 2.2em;
  line-height: 0.6;
  vertical-align: baseline;
}

.title-sub {
  font-size: 0.55em;
  font-weight: 400;
  color: inherit;
  vertical-align: middle;
}

main {
  margin: 0 auto;
  max-width: 1200px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- Sélecteur modèle maison ---- */
.custom-select { position: relative; display: inline-block; min-width: 240px; }
.custom-select-btn {
  width: 100%; font: inherit; font-size: 0.9rem;
  border: 1px solid #bfd0bf; border-radius: 10px;
  padding: 8px 28px 8px 10px; background: #fff;
  text-align: left; cursor: pointer;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 5l3 3 3-3' fill='none' stroke='%23314935' stroke-width='1.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center;
}
.custom-select-panel {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 100;
  background: #fff; border: 1px solid #bfd0bf; border-radius: 10px;
  margin-top: 4px; overflow: hidden; box-shadow: var(--shadow);
}
.custom-select-opt {
  display: block; width: 100%; font: inherit; font-size: 0.85rem;
  padding: 8px 12px; border: none; background: #fff; text-align: left;
  cursor: pointer;
}
.custom-select-opt:hover,
.custom-select-opt.selected { background: var(--accent-soft); }

/* Popup nouvel agencement */
.popup-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.3); z-index: 9999;
  display: flex; align-items: center; justify-content: center;
}
.popup-box {
  background: #fff; border-radius: 14px; padding: 18px 22px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.18); min-width: 280px;
}
.popup-box input {
  width: 100%; border: 1px solid #bfd0bf; border-radius: 10px;
  padding: 8px 10px; font: inherit; font-size: 0.9rem;
}

/* Tous les panneaux : largeur uniforme */
.panel {
  width: 100%;
  max-width: 860px;
  align-self: center;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(6px);
  animation: reveal 0.8s ease;
}

h2 {
  margin: 0 0 8px;
  font-size: 1rem;
}

p {
  margin: 6px 0;
}

input[type="file"] {
  display: none;
}

.file-btn,
button {
  font: inherit;
  border: none;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  display: inline-flex;
  align-items: center;
  width: auto;
}

.file-btn,
button#buildBtn {
  color: #fff;
  background: linear-gradient(120deg, var(--accent), #3e8752);
  box-shadow: 0 7px 14px rgba(36, 102, 57, 0.24);
}

.file-btn:hover,
button:hover {
  transform: translateY(-1px);
}

button.secondary {
  background: #fffaf2;
  border: 1px solid #e9ceb4;
  color: #7e4017;
}

label {
  display: flex;
  flex-direction: column;
  font-size: 0.86rem;
  gap: 4px;
}

select,
input[type="text"],
input[type="number"] {
  width: 100%;
  border: 1px solid #bfd0bf;
  border-radius: 10px;
  padding: 8px;
  background: #ffffff;
  font: inherit;
}

.muted {
  color: #586b5c;
  font-size: 0.85rem;
}

/* --- Éditeur de modèle --- */
#templateEditor h4 {
  margin: 4px 0;
  font-size: 0.85rem;
  color: var(--accent);
}

/* ===== Grille éditeur : forme exacte de l'étiquette ===== */
.grid-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.label-grid {
  width: var(--lw);
  height: var(--lh);
  margin: 10px auto;
  position: relative;
  background: #fff;
  user-select: none;
  overflow: hidden;
  min-width: var(--lw);
}

/* Corps + pointe V */
.label-grid-body {
  position: absolute; top: 0; bottom: 0; left: 0;
  width: var(--gbw); display: flex;
  z-index: 1;
  border: 1px solid #999;
  border-right: none;
  border-radius: 2px 0 0 2px;
}
.label-grid-v {
  position: absolute; top: 0; bottom: 0;
  left: var(--tx); width: var(--tw);
  z-index: 2;
}
.label-grid-v svg {
  width: 100%; height: 100%; display: block;
}

/* Colonnes */
.grid-col {
  height: 100%;
  display: flex; flex-direction: column;
  position: relative;
}
.grid-col-price {
  background: #ffe0e0;
  align-items: center; justify-content: center;
}
.grid-col-price .grid-cell {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-weight: 700;
  color: #a00;
  font-size: 0.65rem;
}
.grid-col-mid  { background: #e8ffe8; }
.grid-col-last { background: #e8e8ff; }

/* Cellules cliquables */
.grid-cell {
  flex: 1;
  border-bottom: 1px dashed #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  color: #555;
  overflow: hidden;
  cursor: pointer;
  min-height: 0;
  padding: 0 1px;
  white-space: nowrap;
}
.grid-cell:hover {
  background: rgba(0,0,0,0.06);
}
.grid-cell.selected {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  z-index: 4;
}
.grid-cell:last-child {
  border-bottom: none;
}

/* Poignées de redimensionnement */
.grid-handle {
  width: 6px;
  cursor: col-resize;
  background: transparent;
  flex-shrink: 0;
  position: relative;
  z-index: 5;
}
.grid-handle:hover,
.grid-handle.active {
  background: rgba(0,0,0,0.15);
}
.grid-handle::after {
  content: '⋮';
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  font-size: 14px; color: #888;
  pointer-events: none;
  line-height: 1;
}

/* Panneau édition cellule */
.cell-editor {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: #fafff5;
}
.cell-editor h4 {
  margin: 0 0 6px;
  font-size: 0.85rem;
}
.cell-editor-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 5px;
  flex-wrap: wrap;
  font-size: 0.8rem;
}
.cell-editor-row select,
.cell-editor-row input[type="number"] {
  padding: 3px 4px;
  font-size: 0.78rem;
  border: 1px solid #bfd0bf;
  border-radius: 6px;
  background: #fff;
}
.cell-editor-row label {
  display: flex;
  align-items: center;
  gap: 2px;
  font-size: 0.75rem;
  white-space: nowrap;
}

/* Single preview */
#singlePreview {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
  display: block;
  text-align: center;
}
#singlePreview .label {
  display: inline-flex;
  text-align: left;
}
#singlePreview .label-body {
  width: var(--tx);
  border: 1px solid var(--accent);
  border-right: none;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
}
#singlePreview .label-pt {
  left: var(--tx);
}

.btn-sm {
  font: inherit;
  font-size: 0.78rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: #fff;
  color: var(--accent);
  padding: 3px 10px;
  cursor: pointer;
}
.btn-sm:hover {
  background: var(--accent);
  color: #fff;
}
.btn-sm.danger {
  color: #c00;
  border-color: #c00;
}
.btn-sm.danger:hover {
  background: #c00;
  color: #fff;
}
.arr-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

#sheetContainer {
  max-height: calc(100vh - 230px);
  overflow: auto;
  border: 1px dashed #adc6af;
  padding: 8px;
  border-radius: 12px;
  background: #f9fcf5;
}

.sheet-hint {
  text-align: center;
  color: #586b5c;
  font-size: 0.9rem;
  padding: 48px 12px;
  margin: 0;
}

/* ---- Page A4 portrait ---- */
.sheet-page {
  width: var(--pw);
  min-height: var(--ph);
  background: #fff;
  border: 1px solid #d9e7d4;
  margin: 0 auto 14px;
  position: relative;
  overflow: hidden;
}

/* ---- Colonnes : top défini en JS selon le modèle ---- */
.sheet-column {
  position: absolute;
  left: 0;
  width: var(--lw);
  display: flex;
  flex-direction: column;
}

.sheet-column.right {
  left: auto;
  right: 0;
}

/* ---- Étiquette : largeur variable selon modèle ---- */
.label {
  width: var(--lw);
  height: var(--lh);
  position: relative;
  display: flex;
  align-items: center;
}

/* Corps (contenu texte) */
.label-body {
  width: var(--bw);
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 2mm;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}

/* Trait horizontal entre étiquettes : s'arrête au début de la pointe */
.label-body::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: var(--tx);
  height: 0;
  border-bottom: 0.15mm solid #999;
}

/* Première étiquette : trait haut aussi */
.sheet-column.left .label:first-child .label-body::before,
.sheet-column.right .label:last-child .label-body::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: var(--tx);
  height: 0;
  border-top: 0.15mm solid #999;
}

/* Pointe en V : SVG inline */
.label-pt {
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--tx);
  width: var(--tw);
  z-index: 2;
}

.label-pt svg {
  width: 100%;
  height: 100%;
  display: block;
  shape-rendering: crispEdges;
}

@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media screen and (max-width: 980px) {
  #sheetContainer {
    max-height: 70vh;
  }

  .sheet-page {
    transform-origin: top center;
    transform: scale(0.48);
    margin-bottom: -155mm;
  }
}

@media print {
  body {
    background: #fff;
    padding: 0;
    margin: 0;
  }

  header,
  .controls,
  #templateEditor {
    display: none !important;
  }

  .actions h2,
  #buildBtn,
  #printBtn,
  #buildInfo {
    display: none !important;
  }

  main {
    display: block;
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0;
    gap: 0;
  }

  .panel.actions {
    width: 100%;
    max-width: none;
    align-self: auto;
    border: none;
    box-shadow: none;
    padding: 0;
    margin: 0;
    background: none;
    border-radius: 0;
  }

  #sheetContainer {
    width: 100%;
    max-height: none;
    overflow: visible;
    border: none;
    padding: 0;
    margin: 0;
    background: #fff;
    border-radius: 0;
  }

  .sheet-page {
    width: var(--pw);
    min-height: var(--ph);
    border: none;
    margin: 0;
    transform: none !important;
    page-break-after: always;
  }

  .sheet-page:last-child {
    page-break-after: auto;
  }
}
