:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #172033;
  --muted: #5b6475;
  --line: #d8d3ca;
  --primary: #3f4f66;
  --primary-strong: #2f3b4d;
  --danger: #b42318;
  --radius: 8px;
  --shadow: 0 8px 24px rgba(31, 41, 51, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
}

.app-header {
  padding: 24px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.eyebrow {
  margin: 0 0 6px;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--primary);
  font-weight: 700;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 8px;
  font-size: 1.7rem;
  line-height: 1.15;
}

h2 {
  margin-bottom: 14px;
  font-size: 1.2rem;
}

h3 {
  margin-bottom: 8px;
  font-size: 1rem;
}

.intro {
  margin-bottom: 0;
  color: var(--muted);
}

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 18px;
  padding: 18px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.controls {
  padding: 18px;
  align-self: start;
  position: sticky;
  top: 12px;
}

label {
  display: block;
  margin-bottom: 6px;
  font-weight: 650;
}

select,
input[type="number"] {
  width: 100%;
  margin-bottom: 14px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  background: white;
  color: var(--text);
}

.actions {
  display: grid;
  gap: 8px;
  margin-bottom: 18px;
}

button {
  border: 0;
  border-radius: 6px;
  padding: 10px 12px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  color: white;
  background: var(--primary);
}

button:hover {
  background: var(--primary-strong);
}

button.secondary {
  background: #64748b;
}

button.secondary:hover {
  background: #475569;
}

.selected-box {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.selected-box p {
  margin-bottom: 10px;
  color: var(--muted);
}

.selected-list {
  display: grid;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}

.gallery-panel {
  padding: 18px;
  min-width: 0;
}

.gallery-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: baseline;
  margin-bottom: 14px;
}

.gallery-head p {
  color: var(--muted);
  margin: 0;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
  gap: 14px;
}

.image-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: white;
  transition: outline 0.15s ease, transform 0.15s ease;
}

.image-card:hover {
  transform: translateY(-1px);
}

.image-card img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  background: white;
  border-bottom: 1px solid var(--line);
}

.image-card-body {
  padding: 10px;
}

.image-card-body label {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 4px;
  font-size: 0.92rem;
  line-height: 1.25;
}

.image-card-body input {
  flex: 0 0 auto;
}

.image-card-body small {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
}

.image-card.is-selected {
  outline: 3px solid rgba(47, 74, 70, 0.35);
}

/* =========================================================
   Options fiche A4
   ========================================================= */

.sheet-options {
  border-top: 1px solid var(--line);
  padding-top: 14px;
  margin-top: 14px;
}

.sheet-options h3 {
  margin-bottom: 12px;
}

.inline-check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 2px 0 14px;
  font-weight: 600;
}

.inline-check input {
  flex: 0 0 auto;
}

.create-sheet-btn {
  width: 100%;
  margin-bottom: 10px;
}

.help-text {
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.selected-empty {
  color: var(--muted);
  font-style: italic;
}

.selected-item {
  padding: 4px 0;
  border-bottom: 1px solid rgba(216, 211, 202, 0.55);
}

/* =========================================================
   Correction galerie compacte
   ========================================================= */

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(145px, 180px));
  gap: 14px;
  align-items: start;
}

.image-card img {
  display: block;
  width: 100%;
  height: 170px;
  object-fit: contain;
  background: white;
  border-bottom: 1px solid var(--line);
}

/* =========================================================
   Fiche A4 imprimable
   ========================================================= */

.sheet-body {
  margin: 0;
  background: #ece9e2;
  color: var(--text);
}

.sheet-toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 56px;
  padding: 10px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.toolbar-link {
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
}

.toolbar-link:hover {
  text-decoration: underline;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
}

.toolbar-actions button {
  padding: 8px 12px;
}

.a4-sheet {
  width: 210mm;
  min-height: 297mm;
  margin: 18px auto;
  padding: 14mm;
  background: white;
  box-shadow: 0 12px 36px rgba(31, 41, 51, 0.18);
}

.sheet-header {
  text-align: center;
  margin-bottom: 10mm;
}

.sheet-header h1 {
  margin: 0 0 4mm;
  font-size: 24pt;
  line-height: 1.15;
}

.sheet-header p {
  margin: 0 auto;
  max-width: 150mm;
  color: var(--muted);
  font-size: 11pt;
}

.sheet-grid {
  display: grid;
  gap: 6mm;
}

.sheet-grid-2x3 {
  grid-template-columns: repeat(2, 1fr);
}

.sheet-grid-3x3 {
  grid-template-columns: repeat(3, 1fr);
}

.sheet-grid-4x4 {
  grid-template-columns: repeat(4, 1fr);
  gap: 4mm;
}

.sheet-card {
  border: 1px solid #d8d3ca;
  border-radius: 4mm;
  padding: 4mm;
  background: white;
  break-inside: avoid;
}

.sheet-image-box {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
}

.sheet-image-box img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.sheet-caption {
  margin: 3mm 0 0;
  text-align: center;
  font-weight: 800;
  font-size: 13pt;
}

.sheet-grid-4x4 .sheet-caption {
  font-size: 10pt;
}

.sheet-empty {
  margin-top: 30mm;
  padding: 20mm;
  border: 1px dashed var(--line);
  text-align: center;
  color: var(--muted);
}

@page {
  size: A4 portrait;
  margin: 0;
}

@media print {
  html,
  body {
    width: 210mm;
    min-height: 297mm;
    background: white;
  }

  .no-print {
    display: none !important;
  }

  .a4-sheet {
    width: 210mm;
    min-height: 297mm;
    margin: 0;
    padding: 12mm;
    box-shadow: none;
  }

  .sheet-header {
    margin-bottom: 8mm;
  }

  .sheet-header h1 {
    font-size: 22pt;
  }

  .sheet-card {
    box-shadow: none;
  }
}

@media (max-width: 900px) {
  .sheet-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar-actions {
    align-items: flex-start;
    flex-direction: column;
  }

  .a4-sheet {
    width: calc(100vw - 24px);
    min-height: auto;
    padding: 18px;
    margin: 12px;
  }
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .controls {
    position: static;
  }
}