* {
  box-sizing: border-box;
}

:root {
  --bg: #e9e6e1;
  --panel: #ffffff;
  --ink: #2c2a27;
  --muted: #8a857c;
  --accent: #4a6741;
  --accent-dark: #3a5334;
  --danger: #a33;
  --sheet-scale: 0.705;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

/* ---------- Layout ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid #ddd8d1;
  flex-wrap: wrap;
}
header h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.5px;
}
header .spacer {
  flex: 1;
}
header .info {
  color: var(--muted);
  font-size: 13px;
}

.btn {
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid #cbc5bc;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 7px 14px;
  transition:
    background 0.15s,
    transform 0.05s;
}
.btn:hover {
  background: #f6f4f0;
}
.btn:active {
  transform: translateY(1px);
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent-dark);
  color: #fff;
}
.btn.primary:hover {
  background: var(--accent-dark);
}
.btn.small {
  padding: 4px 9px;
  font-size: 13px;
  border-radius: 6px;
}

main {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ---------- Drop zone / empty state ---------- */
#dropZone {
  flex: 1;
  margin: 16px;
  border: 3px dashed #c9c2b8;
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.5);
  text-align: center;
  cursor: pointer;
  transition:
    border-color 0.15s,
    background 0.15s;
}
#dropZone.dragover {
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.9);
  color: var(--accent);
}
#dropZone .big {
  font-size: 40px;
}
#dropZone h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}
#dropZone p {
  margin: 0;
  font-size: 14px;
}

/* ---------- Workspace ---------- */
#workspace {
  flex: 1;
  display: none;
  min-width: 0;
}

/* ---------- Tab bar (mobile) ---------- */
.tabbar {
  display: none;
}

.panel {
  background: var(--panel);
  overflow-y: auto;
}
.panel.editor {
  width: 360px;
  border-right: 1px solid #ddd8d1;
  flex-shrink: 0;
}

.panel-title {
  position: sticky;
  top: 0;
  background: var(--panel);
  padding: 12px 16px 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  border-bottom: 1px solid #eee9e2;
  z-index: 2;
}

.photo-list {
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.photo-item {
  display: flex;
  gap: 10px;
  border: 1px solid #e2dcd4;
  border-radius: 10px;
  padding: 8px;
  background: #fdfcfa;
}
.photo-item.dragging {
  opacity: 0.5;
}
.photo-thumb {
  width: 66px;
  height: 66px;
  flex-shrink: 0;
  background: #ece9e4;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.photo-fields {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}
.photo-fields label {
  font-size: 11px;
  color: var(--muted);
}
.photo-fields input {
  width: 100%;
  font: inherit;
  font-size: 13px;
  border: 1px solid #d8d2c9;
  border-radius: 6px;
  padding: 4px 7px;
}
.photo-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.photo-actions .btn.small {
  padding: 2px 8px;
  font-size: 12px;
}

/* ---------- Preview ---------- */
.panel.preview {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 16px;
  overflow: auto;
}
.sheets {
  display: flex;
  flex-direction: column;
  gap: 28px;
  align-items: center;
}

.sheet-wrap {
  width: calc(210mm * var(--sheet-scale));
  height: calc(297mm * var(--sheet-scale));
  overflow: hidden;
  border-radius: 4px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  background: #fff;
  flex-shrink: 0;
}
.sheet {
  width: 210mm;
  height: 297mm;
  background: #fff;
  padding: 0mm;
  transform-origin: top left;
  transform: scale(var(--sheet-scale));
}

.sheet-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 85mm);
  justify-content: start;
  align-content: start;
  gap: 0;
  height: 100%;
}

/* ---------- Polaroid ---------- */
.polaroid {
  width: 100%;
  height: 85mm;
  background: #fff;
  padding: 2.8mm;
  display: flex;
  flex-direction: column;
  border-radius: 0;
  box-shadow: none;
  overflow: hidden;
  border: 1px dashed #ccc;
}
.polaroid .photo {
  width: 100%;
  aspect-ratio: 1 / 1;
  flex-shrink: 0;
  background: #eae7e2;
  border-radius: 0;
  overflow: hidden;
}
.polaroid .photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.polaroid .photo.crop-click {
  position: relative;
  cursor: pointer;
}
.polaroid .photo.crop-click::after {
  content: "⤢ cadrer";
  position: absolute;
  right: 4mm;
  bottom: 4mm;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-family: "Segoe UI", sans-serif;
  font-size: 2.8mm;
  padding: 0.8mm 1.6mm;
  border-radius: 1mm;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.polaroid .photo.crop-click:hover::after {
  opacity: 1;
}
.polaroid .caption {
  flex-shrink: 0;
  height: 12mm;
  display: flex;
  flex-direction: column;
  padding: 1mm 1mm 0.5mm;
  margin-top: 2mm;
  flex: 1;
}
.polaroid .caption .p-title {
  font-family: "Segoe Script", "Bradley Hand", "Comic Sans MS", cursive;
  font-size: 4.5mm;
  font-weight: 600;
  line-height: 1.2;
  color: #2b2824;
  text-align: center;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.polaroid .caption .p-date {
  font-family: "Segoe Script", "Bradley Hand", "Comic Sans MS", cursive;
  font-size: 2.4mm;
  color: #6d675e;
  margin-top: auto;
  align-self: flex-end;
}
.polaroid.empty {
  background: transparent;
  box-shadow: none;
}
.polaroid.empty-click {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 1px dashed #c9c2b8;
}
.polaroid.empty-click:hover {
  background: #f6f3ee;
}
.empty-plus {
  font-size: 12mm;
  color: #c9c2b8;
  line-height: 1;
}
.polaroid.empty-click:hover .empty-plus {
  color: var(--accent);
}

/* ---------- Crop editor ---------- */
.crop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.crop-modal {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}
.crop-stage {
  width: min(70vh, 80vw, 440px);
  aspect-ratio: 1 / 1;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: #111;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.crop-stage.dragging {
  cursor: grabbing;
}
.crop-stage img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 50% 50%;
  pointer-events: none;
}
.crop-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  html,
  body {
    height: auto;
    min-height: 100vh;
  }
  .app {
    height: auto;
    min-height: 100vh;
  }
  header {
    padding: 8px 10px;
    gap: 8px;
  }
  header h1 {
    font-size: 16px;
    width: 100%;
  }
  header .info {
    order: 5;
    width: 100%;
  }
  .tabbar {
    display: flex;
    gap: 6px;
    padding: 8px 10px;
    background: var(--panel);
    border-bottom: 1px solid #ddd8d1;
    position: sticky;
    top: 0;
    z-index: 20;
  }
  .tabbar .btn {
    flex: 1;
    padding: 10px 0;
    text-align: center;
  }
  main {
    flex-direction: column;
  }
  #workspace {
    flex-direction: column;
    min-height: 0;
  }
  #workspace.view-sheets .panel.editor {
    display: none;
  }
  #workspace.view-list .panel.preview {
    display: none;
  }
  #workspace.view-list .panel.editor {
    display: block;
    width: 100%;
    height: calc(100vh - 110px);
    border-right: none;
  }
  .panel.preview {
    padding: 16px 12px;
    min-height: 0;
  }
  .sheets {
    gap: 20px;
  }
  .photo-actions {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
  }
  .photo-item {
    gap: 8px;
  }
  .photo-thumb {
    width: 54px;
    height: 54px;
  }
  .crop-stage {
    width: min(80vw, 80vw);
  }
}

/* ---------- Print ---------- */
@media print {
  @page {
    size: A4;
    margin: 0;
  }
  html,
  body {
    background: #fff;
  }
  header,
  .panel.editor {
    display: none !important;
  }
  .app,
  main {
    display: block;
    height: auto;
  }
  .panel.preview {
    display: block;
    padding: 0;
    overflow: visible;
  }
  .sheets {
    display: block;
    gap: 0;
  }
  .sheet-wrap {
    width: auto;
    height: auto;
    overflow: visible;
    box-shadow: none;
    border-radius: 0;
    page-break-after: always;
  }
  .sheet {
    transform: none;
    padding: 8mm;
  }
  .polaroid {
    box-shadow: none;
    border-radius: 0;
  }
  .polaroid.empty,
  .polaroid.empty-click {
    display: none;
  }
}
