:root {
  --paper: #f5f2ea;
  --surface: #fffdf7;
  --ink: #211d17;
  --muted: #6d675b;
  --faint: #928b7d;
  --line: #e0d9ca;
  --line-strong: #c9bfa9;
  --accent: #a1502f;
  --accent-ink: #7f3d22;
  --amber: #b07d16;
  --green: #3f7a51;
  --red: #ab3a34;
  --focus: rgba(161, 80, 47, 0.28);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #17150f;
    --surface: #201d15;
    --ink: #ece6d8;
    --muted: #a49d8d;
    --faint: #837c6d;
    --line: #322d22;
    --line-strong: #453f31;
    --accent: #d98a5f;
    --accent-ink: #e8a37c;
    --amber: #d6a63e;
    --green: #6fb183;
    --red: #db6f68;
    --focus: rgba(217, 138, 95, 0.32);
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 26px 26px;
  background-position: -1px -1px;
  -webkit-font-smoothing: antialiased;
}

.wrap {
  max-width: 660px;
  margin: 0 auto;
  padding: clamp(2rem, 6vw, 4rem) 1.25rem 4rem;
}

/* ---- Masthead ---- */

.masthead {
  margin-bottom: 2.5rem;
}

.masthead .kicker {
  margin: 0 0 0.35rem;
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--faint);
}

h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(1.9rem, 5vw, 2.6rem);
  line-height: 1.08;
  letter-spacing: -0.01em;
  margin: 0;
}

.masthead .lede {
  margin: 0.6rem 0 0;
  color: var(--muted);
  max-width: 46ch;
}

/* ---- Panels ---- */

.panel {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  box-shadow: 0 1px 0 rgba(33, 29, 23, 0.02);
}

/* ---- Upload form ---- */

form.upload {
  padding: 1.5rem 1.5rem 1.6rem;
  margin-bottom: 2.75rem;
}

.field {
  margin-bottom: 1.15rem;
}

.field label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--ink);
  margin-bottom: 0.4rem;
}

.field .hint {
  font-weight: 400;
  color: var(--faint);
}

input[type="text"] {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 0.6rem 0.7rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus);
}

input[type="file"] {
  width: 100%;
  font: inherit;
  font-size: 0.9rem;
  color: var(--muted);
  padding: 0.55rem 0;
}

input[type="file"]::file-selector-button {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 0.45rem 0.85rem;
  margin-right: 0.85rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

input[type="file"]::file-selector-button:hover {
  border-color: var(--accent);
  color: var(--accent-ink);
}

button[type="submit"] {
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 1px solid var(--accent-ink);
  border-radius: 7px;
  padding: 0.62rem 1.2rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}

button[type="submit"]:hover {
  background: var(--accent-ink);
}

button[type="submit"]:active {
  transform: translateY(1px);
}

button[type="submit"]:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus);
}

button[type="submit"]:disabled {
  opacity: 0.55;
  cursor: default;
}

/* ---- Upload progress ---- */

.progress {
  margin-top: 1.25rem;
}

.progress-track {
  height: 8px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.2s ease;
}

.progress-label {
  margin: 0.55rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.progress-label.is-error {
  color: var(--red);
}

/* ---- Maps list ---- */

.maps-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.maps-head h2 {
  font-family: var(--serif);
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
}

.maps-head .count {
  font-size: 0.82rem;
  color: var(--faint);
}

ul.maps {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--line);
}

ul.maps li {
  border-bottom: 1px solid var(--line);
}

ul.maps a.row,
ul.maps .row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.15rem 1rem;
  padding: 0.85rem 0.4rem;
  text-decoration: none;
  color: inherit;
}

ul.maps a.row {
  transition: background 0.12s;
}

ul.maps a.row:hover {
  background: var(--surface);
}

.row .title {
  font-family: var(--serif);
  font-size: 1.05rem;
  color: var(--ink);
}

.row .meta {
  grid-column: 1;
  font-size: 0.78rem;
  color: var(--faint);
  font-variant-numeric: tabular-nums;
}

.row .action {
  grid-column: 2;
  grid-row: 1 / span 2;
  justify-self: end;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent-ink);
}

.row .action.muted {
  color: var(--faint);
  font-weight: 500;
}

/* ---- Status pill ---- */

.status {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--muted);
}

.status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--faint);
  flex: none;
}

.status.is-queued,
.status.is-running { color: var(--amber); }
.status.is-queued::before,
.status.is-running::before {
  background: var(--amber);
  animation: pulse 1.4s ease-in-out infinite;
}

.status.is-tiles_ready,
.status.is-done { color: var(--green); }
.status.is-tiles_ready::before,
.status.is-done::before { background: var(--green); }

.status.is-error { color: var(--red); }
.status.is-error::before { background: var(--red); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

@media (prefers-reduced-motion: reduce) {
  .status::before { animation: none !important; }
}

/* ---- Empty state ---- */

.empty {
  padding: 2.25rem 1.5rem;
  text-align: center;
  color: var(--muted);
}

.empty .mark {
  font-family: var(--serif);
  font-size: 1.6rem;
  color: var(--line-strong);
  margin-bottom: 0.35rem;
}

/* ---- Status page ---- */

.tracker {
  padding: 1.75rem 1.5rem;
}

.tracker .status {
  font-size: 0.95rem;
  margin-bottom: 1rem;
}

.tracker .message {
  color: var(--muted);
  min-height: 1.5em;
  margin: 0 0 1.25rem;
}

.tracker .elapsed {
  font-size: 0.82rem;
  color: var(--faint);
  font-variant-numeric: tabular-nums;
}

.tracker .cta {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
}

/* ---- Links & footer ---- */

a {
  color: var(--accent-ink);
  text-decoration-color: var(--line-strong);
  text-underline-offset: 2px;
}

a:hover {
  text-decoration-color: var(--accent);
}

.backlink {
  display: inline-block;
  margin-top: 2rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-decoration: none;
}

.backlink::before {
  content: "\2190";
  margin-right: 0.4rem;
}

.backlink:hover {
  color: var(--accent-ink);
}

/* ---- Error box ---- */

.error-box {
  background: color-mix(in srgb, var(--red) 8%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--red) 35%, var(--line));
  color: var(--red);
  padding: 0.9rem 1.1rem;
  border-radius: 8px;
}

/* ---- Footnote ---- */

.footnote {
  margin-top: 2rem;
  text-align: center;
  font-size: 0.72rem;
  color: var(--faint);
}

/* ---- Lien secondaire (accueil ↔ atelier) ---- */

.alt-link {
  margin: 1.25rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

/* ---- Atelier d'assemblage (compose.php) ---- */

.wrap-wide {
  max-width: 960px;
}

.compose-controls {
  padding: 1.5rem 1.5rem 1.6rem;
  margin-bottom: 1.25rem;
}

.compose-note {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: var(--red);
}

.field-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--ink);
  margin-bottom: 0.4rem;
}

.dropzone {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.85rem;
  padding: 1rem 1.1rem;
  border: 1.5px dashed var(--line-strong);
  border-radius: 9px;
  background: var(--paper);
  transition: border-color 0.15s, background 0.15s;
}

.dropzone.is-dragover {
  border-color: var(--accent);
  border-style: solid;
  background: color-mix(in srgb, var(--accent) 8%, var(--paper));
}

.dropzone .dropzone-hint {
  font-size: 0.85rem;
  color: var(--faint);
}

.dropzone input[type="file"] {
  width: auto;
  padding: 0;
}

.board.is-dragover {
  border-color: var(--accent);
  border-style: solid;
  background-color: color-mix(in srgb, var(--accent) 6%, var(--surface));
}

.board-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.board-info {
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Boutons secondaires (téléchargement) : contour, pour ne pas concurrencer
   l'action principale « Assembler & générer ». */
.board-toolbar .btn-secondary {
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-strong);
}

.board-toolbar .btn-secondary:hover:not(:disabled) {
  background: var(--paper);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.board-toolbar button {
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 1px solid var(--accent-ink);
  border-radius: 7px;
  padding: 0.55rem 1.1rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}

.board-toolbar button:hover:not(:disabled) {
  background: var(--accent-ink);
}

.board-toolbar button:active:not(:disabled) {
  transform: translateY(1px);
}

.board-toolbar button:disabled {
  opacity: 0.5;
  cursor: default;
}

.board {
  position: relative;
  height: 62vh;
  min-height: 340px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 22px 22px;
  overflow: hidden;
  touch-action: none;
}

.board-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 1rem;
  text-align: center;
  color: var(--faint);
  pointer-events: none;
}

.piece {
  position: absolute;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  border: 1px solid rgba(33, 29, 23, 0.25);
  box-shadow: 0 1px 4px rgba(33, 29, 23, 0.15);
  cursor: grab;
  user-select: none;
  touch-action: none;
}

.piece.is-dragging {
  cursor: grabbing;
  outline: 2px solid var(--accent);
  box-shadow: 0 6px 18px rgba(33, 29, 23, 0.3);
  z-index: 10;
}

.piece.is-loading {
  opacity: 0.6;
}

.piece.is-failed {
  outline: 2px solid var(--red);
}

.piece-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  min-width: 1.3em;
  padding: 0 0.35em;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.4;
  text-align: center;
  color: #fff;
  background: rgba(33, 29, 23, 0.7);
  border-radius: 4px;
}

.piece-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 2px 6px;
  font-size: 0.68rem;
  line-height: 1.35;
  color: #fff;
  background: rgba(33, 29, 23, 0.62);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit;
}

.piece-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  padding: 0;
  font: inherit;
  font-size: 0.8rem;
  line-height: 1;
  color: #fff;
  background: rgba(33, 29, 23, 0.7);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s, background 0.12s;
}

.piece:hover .piece-remove,
.piece-remove:focus-visible {
  opacity: 1;
}

.piece-remove:hover {
  background: var(--red);
}

/* ---- Lien de reprise (brouillon multi-sessions) ---- */

.resume-link {
  display: flex;
  gap: 0.85rem;
  margin-top: 1.25rem;
  padding: 0.9rem 1rem;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: color-mix(in srgb, var(--amber) 8%, var(--surface));
}

.resume-icon {
  font-size: 1.2rem;
  line-height: 1.4;
}

.resume-body {
  flex: 1;
  min-width: 0;
}

.resume-title {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink);
}

.resume-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.resume-url {
  flex: 1;
  min-width: 12rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.8rem;
  color: var(--muted);
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 0.45rem 0.6rem;
}

#resume-copy {
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s, color 0.15s;
}

#resume-copy:hover {
  border-color: var(--accent);
  color: var(--accent-ink);
}
