:root {
  --bg: #fafafa;
  --bg-soft: #f5f5f5;
  --panel: #ffffff;
  --panel-alt: #ffffff;
  --ink: #333333;
  --muted: #666666;
  --accent: #333333;
  --accent-color: #1a6b3c;
  --line: #e0e0e0;
  --line-strong: #d7d7d7;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --bbox-color: #00d44b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

/* --- Topbar --- */
.topbar {
  padding: 1rem 1.25rem 0.9rem;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

.topbar h1 {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: 0.01em;
  font-weight: 600;
}

.topbar p {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* --- Layout --- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(280px, 420px);
  gap: 0.9rem;
  padding: 1rem 1.1rem 1.1rem;
}

/* --- Panels --- */
.stage-panel,
.prompt-panel {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.stage-panel {
  padding: 0.9rem;
  min-height: 70vh;
}

.prompt-panel {
  padding: 0.9rem;
  display: grid;
  gap: 0.6rem;
  align-content: start;
  background: var(--panel-alt);
}

/* --- Drop zone --- */
.drop-zone {
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
  min-height: 64vh;
  display: grid;
  place-content: center;
  gap: 0.8rem;
  text-align: center;
  background: #fcfcfc;
  transition: border-color 0.2s ease, transform 0.2s ease;
  color: var(--muted);
}

.drop-zone.dragover {
  border-color: var(--accent);
  transform: scale(1.005);
}

/* --- Canvas wrap --- */
.canvas-wrap {
  display: grid;
  gap: 0.7rem;
  height: 100%;
  animation: fade-up 0.3s ease;
}

.canvas-wrap.hidden,
.drop-zone.hidden {
  display: none;
}

.canvas-container {
  position: relative;
  width: 100%;
}

#mainCanvas {
  width: 100%;
  height: min(74vh, 760px);
  border: 1px solid var(--line);
  border-radius: 10px;
  background:
    repeating-conic-gradient(#f8f8f8 0% 25%, #f2f2f2 0% 50%) 50% / 26px 26px;
  display: block;
}

/* --- Loading overlay --- */
.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.82);
  border-radius: 10px;
  gap: 0.5rem;
  z-index: 10;
}

.loading-overlay.hidden {
  display: none;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-overlay p {
  margin: 0;
  font-size: 0.92rem;
  color: var(--ink);
}

.loading-detail {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.78rem !important;
  color: var(--muted) !important;
  max-width: 80%;
  text-align: center;
}

/* --- Toolbar --- */
.toolbar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* --- Buttons --- */
.btn {
  border: 1px solid var(--line-strong);
  background: #ffffff;
  color: var(--ink);
  border-radius: 10px;
  padding: 0.55rem 1rem;
  font: 500 0.88rem "Space Grotesk", sans-serif;
  cursor: pointer;
  transition: border-color 0.12s ease, background-color 0.12s ease, color 0.12s ease;
}

.btn:hover {
  border-color: #bdbdbd;
  background: #f2f2f2;
  color: #1f1f1f;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--ink);
  color: #ffffff;
  border-color: var(--ink);
}

.btn.primary:hover {
  background: #1f1f1f;
  border-color: #1f1f1f;
}

.btn.primary:disabled {
  background: #8a8a8a;
  border-color: #8a8a8a;
}

/* --- Labels --- */
label {
  font-size: 0.92rem;
  color: #4a4a4a;
}

/* --- Textarea --- */
textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  padding: 0.8rem;
  font: 500 0.92rem/1.35 "IBM Plex Mono", monospace;
  color: #000000;
  background: #ffffff;
}

textarea:focus {
  outline: none;
  border-color: #bdbdbd;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.06);
}

/* --- Status --- */
.status {
  min-height: 1.2rem;
  font-size: 0.9rem;
  color: var(--muted);
  margin: 0;
}

/* --- Result --- */
.result {
  margin: 0;
  min-height: 100px;
  max-height: 200px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.8rem;
  background: #ffffff;
  color: #333333;
  overflow-y: auto;
  font: 500 0.9rem/1.4 "IBM Plex Mono", monospace;
}

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

/* --- Log output --- */
.log-output {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fcfcfc;
  overflow: hidden;
}

.log-label {
  margin: 0;
  padding: 0.45rem 0.8rem;
  background: #f5f5f5;
  font-size: 0.78rem;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

.log-entries {
  max-height: 200px;
  overflow-y: auto;
  padding: 0.5rem 0.8rem;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.76rem;
  line-height: 1.5;
  color: #555;
}

.log-entry {
  padding: 0.15rem 0;
  border-bottom: 1px solid #f0f0f0;
}

.log-entry:last-child {
  border-bottom: none;
}

.log-entry.found {
  color: #1a6b3c;
}

.log-entry.not-found {
  color: #a72f16;
}

.log-entry.info {
  color: #4a6fa5;
}

/* --- Query details (requetes au modele) --- */
.query-details {
  margin: 0 1.1rem 1.5rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #ffffff;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.query-details__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
  background: #fafafa;
}

.query-details__header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
}

.query-count {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.8rem;
  color: var(--muted);
}

.query-entries {
  max-height: 600px;
  overflow-y: auto;
  padding: 0;
}

.query-entries .muted {
  padding: 1rem;
  color: var(--muted);
  text-align: center;
}

.query-entry {
  border-bottom: 1px solid var(--line);
  font-size: 0.82rem;
}

.query-entry:last-child {
  border-bottom: none;
}

.query-entry__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 1rem;
  background: #fafafa;
  cursor: pointer;
  user-select: none;
  gap: 0.5rem;
}

.query-entry__header:hover {
  background: #f0f0f0;
}

.query-entry__title {
  font-weight: 600;
  font-size: 0.84rem;
  color: var(--ink);
  flex: 1;
}

.query-entry__badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  font-family: "IBM Plex Mono", monospace;
}

.query-entry__badge.yes {
  background: #d4f5e0;
  color: #1a6b3c;
}

.query-entry__badge.no {
  background: #fdd;
  color: #a72f16;
}

.query-entry__arrow {
  font-size: 0.7rem;
  color: var(--muted);
  transition: transform 0.2s;
}

.query-entry.open .query-entry__arrow {
  transform: rotate(180deg);
}

.query-entry__body {
  display: none;
  padding: 0.6rem 1rem;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.78rem;
  line-height: 1.5;
  color: #444;
  background: #fdfdfd;
}

.query-entry.open .query-entry__body {
  display: block;
}

.query-entry__body .label {
  font-weight: 600;
  color: #888;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 0.5rem;
  margin-bottom: 0.15rem;
}

.query-entry__body .label:first-child {
  margin-top: 0;
}

.query-entry__body .value {
  color: #333;
  word-break: break-word;
}

.query-entry__body .crop-dims {
  color: var(--muted);
  font-size: 0.72rem;
}

.query-entry__layout {
  display: flex;
  gap: 0.8rem;
  align-items: flex-start;
}

.query-entry__img-wrap {
  flex-shrink: 0;
  width: 120px;
  height: 120px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.query-entry__img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.query-entry__info {
  flex: 1;
  min-width: 0;
}

.prompt-full {
  white-space: pre-wrap;
  font-size: 0.74rem;
  line-height: 1.4;
  color: #555;
  background: #f9f9f9;
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  border: 1px solid #eee;
}

/* --- Animations --- */
@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .stage-panel {
    min-height: auto;
  }

  #mainCanvas {
    height: min(50vh, 400px);
  }
}
