/* --- Reset & base --- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

[hidden] {
  display: none !important;
}

:root {
  --bg: #f6f2ed;
  --bg-card: #ffffff;
  --bg-card-hover: #fdfcfa;
  --border: #e2dcd4;
  --border-focus: #b8a990;
  --text: #2c2520;
  --text-muted: #7a6e62;
  --text-hint: #a09585;
  --accent: #c2714f;
  --accent-hover: #a85d3f;
  --accent-light: #f5ebe4;
  --success: #5a8a6a;
  --error: #c25050;
  --working: #c2964f;
  --radius: 8px;
  --shadow-sm: 0 1px 3px rgba(44, 37, 32, 0.06);
  --shadow-md: 0 4px 12px rgba(44, 37, 32, 0.08);
  --font-display: 'Source Serif 4', Georgia, serif;
  --font-body: 'DM Sans', system-ui, sans-serif;
}

html {
  font-size: 15px;
}

body {
  font-family: var(--font-body);
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  line-height: 1.55;
}

.busy-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(246, 242, 237, 0.22);
  backdrop-filter: grayscale(0.08) blur(0.75px);
  display: grid;
  place-items: center;
}

.busy-overlay-panel {
  display: flex;
  align-items: flex-start;
  gap: 0.85rem;
  padding: 1rem 1.1rem;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(44, 37, 32, 0.08);
  box-shadow: 0 10px 28px rgba(44, 37, 32, 0.1);
  color: var(--text);
  max-width: min(38rem, calc(100vw - 2rem));
}

.busy-overlay.panel-dismissed .busy-overlay-panel {
  display: none;
}

.busy-spinner {
  width: 1rem;
  height: 1rem;
  border-radius: 999px;
  border: 2px solid rgba(194, 113, 79, 0.22);
  border-top-color: var(--accent);
  animation: busy-spin 0.8s linear infinite;
}

.busy-copy {
  min-width: 0;
}

.busy-overlay-title {
  font-weight: 600;
  margin-bottom: 0.2rem;
}

.busy-overlay-message {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
}

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

/* --- Layout --- */
.pipeline-flow {
  max-width: 1400px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.1rem;
  padding: 1.5rem 1.25rem 3rem;
}

/* --- Cards --- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow-sm);
}

.card-prompt,
.card-description {
  display: flex;
  flex-direction: column;
}

.status-monitor {
  grid-column: 1 / -1;
  position: sticky;
  top: 1rem;
  z-index: 10;
}

.stage-card {
  position: relative;
  min-width: 0;
}

.stage-marker {
  position: absolute;
  right: 1rem;
  top: 0.95rem;
  width: 2.35rem;
  height: 2.35rem;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: #efe5dc;
  border: 1px solid rgba(194, 113, 79, 0.18);
  color: var(--text-muted);
  font-weight: 700;
  font-size: 0.88rem;
  box-shadow: 0 6px 18px rgba(44, 37, 32, 0.06);
}

.stage-card .card {
  height: 100%;
  padding-top: 3.9rem;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s;
}

.stage-card.is-active .card {
  border-color: rgba(194, 113, 79, 0.48);
  box-shadow: 0 10px 28px rgba(194, 113, 79, 0.11);
}

.stage-card.is-active .stage-marker {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.stage-card.is-complete .stage-marker {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
}

.card-library {
  grid-column: 1 / -1;
}

.card h2 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05rem;
  margin-bottom: 0.4rem;
  color: var(--text);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.card-header h2 {
  margin-bottom: 0;
}

.card-controls-inline {
  margin-bottom: 0.5rem;
}

.card-hint {
  font-size: 0.8rem;
  color: var(--text-hint);
  margin-bottom: 0.75rem;
}

.card-hint code {
  background: var(--accent-light);
  padding: 0.1em 0.4em;
  border-radius: 3px;
  font-size: 0.78rem;
  color: var(--accent);
}

.camera-panel {
  margin-bottom: 1rem;
  padding: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: linear-gradient(180deg, #fcfaf7 0%, #f4ede6 100%);
}

.upload-panel {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.file-input {
  flex: 1 1 16rem;
  min-width: 0;
  font: inherit;
}

.camera-preview {
  position: relative;
  aspect-ratio: 4 / 3;
  width: 100%;
  border-radius: 6px;
  overflow: hidden;
  background: #1f1a16;
  border: 1px solid rgba(44, 37, 32, 0.15);
}

.camera-preview video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.camera-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.78);
  font-size: 0.85rem;
  letter-spacing: 0.01em;
  text-align: center;
  padding: 1rem;
}

.camera-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

.camera-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.65rem;
}

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

.selection-count {
  font-size: 0.82rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.selection-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

.stage-actions {
  margin-top: 0.85rem;
  display: flex;
  gap: 0.75rem;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.stage-select {
  flex: 1 1 10rem;
  min-width: 0;
  max-width: 100%;
}

.pipeline-select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.7rem 0.75rem;
}

.pipeline-path {
  margin-top: 0.6rem;
  font-size: 0.78rem;
  color: var(--text-hint);
  word-break: break-all;
}

.debug-bar {
  margin-top: 0.9rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.debug-summary {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.debug-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(44, 37, 32, 0.34);
  backdrop-filter: blur(2px);
}

.modal-dialog {
  position: relative;
  z-index: 1;
  max-width: 32rem;
  margin: 8vh auto 0;
  padding: 0 1rem;
}

.modal-dialog-wide {
  max-width: 72rem;
}

.modal-dialog-image {
  max-width: min(92vw, 70rem);
  margin-top: 4vh;
}

.modal-card {
  box-shadow: 0 18px 48px rgba(44, 37, 32, 0.18);
}

.image-modal-card {
  padding-bottom: 1rem;
}

.pipeline-editor-card {
  display: flex;
  flex-direction: column;
  min-height: 78vh;
}

.output-modal-body {
  max-height: 78vh;
  overflow: auto;
  display: grid;
  place-items: center;
}

.output-modal-body img {
  display: block;
  width: auto;
  max-width: 100%;
  max-height: 72vh;
  height: auto;
  border-radius: 6px;
  box-shadow: var(--shadow-md);
}

.card-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.settings-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.45rem;
}

.pipeline-editor-toolbar {
  display: grid;
  grid-template-columns: minmax(14rem, 20rem) minmax(18rem, 1fr);
  gap: 1rem;
  align-items: end;
}

.pipeline-editor-create-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.pipeline-editor-select-group,
.pipeline-editor-create-group {
  min-width: 0;
}

.pipeline-editor-textarea {
  min-height: 24rem;
  flex: 1 1 auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.modal-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

.settings-toggle {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin-top: 0.9rem;
  color: var(--text);
  font-size: 0.88rem;
}

.debug-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 50vh;
  overflow: auto;
}

.debug-item {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem 0.85rem;
  background: #fcfaf7;
}

.debug-meta {
  font-size: 0.72rem;
  color: var(--text-hint);
  margin-bottom: 0.35rem;
}

.debug-message {
  font-size: 0.85rem;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

/* --- Image grid --- */
.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.6rem;
  min-height: 60px;
}

.image-grid .empty-state {
  grid-column: 1 / -1;
}

.image-thumb {
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
  position: relative;
  cursor: pointer;
  padding: 0;
  text-align: left;
  appearance: none;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.1s;
}

.image-thumb:hover {
  border-color: var(--border-focus);
}

.image-thumb.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(194, 113, 79, 0.22);
}

.image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.image-thumb .thumb-label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(44, 37, 32, 0.7);
  color: #fff;
  font-size: 0.65rem;
  padding: 0.2rem 0.35rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.image-thumb .thumb-check {
  position: absolute;
  top: 0.35rem;
  left: 0.35rem;
  background: rgba(44, 37, 32, 0.78);
  color: #fff;
  font-size: 0.62rem;
  padding: 0.12rem 0.35rem;
  border-radius: 999px;
}

.image-thumb.selected .thumb-check {
  background: var(--accent);
}

.image-thumb .thumb-delete {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  background: rgba(194, 80, 80, 0.92);
  color: #fff;
  font-size: 0.62rem;
  padding: 0.12rem 0.35rem;
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.2s;
}

.image-thumb:hover .thumb-delete,
.image-thumb:focus-visible .thumb-delete,
.image-thumb.selected .thumb-delete {
  opacity: 1;
}

.image-picker-grid {
  max-height: 60vh;
  overflow: auto;
  padding-right: 0.2rem;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.75rem;
}

.library-grid .empty-state {
  grid-column: 1 / -1;
}

.library-item {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.1s;
}

.library-item:hover {
  border-color: var(--border-focus);
  box-shadow: var(--shadow-sm);
}

.library-item img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.library-label {
  display: block;
  padding: 0.55rem 0.65rem;
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* --- Textareas --- */
textarea {
  width: 100%;
  font-family: var(--font-body);
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  resize: vertical;
  transition: border-color 0.2s;
  min-height: 12rem;
  flex: 1 1 auto;
}

textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(184, 169, 144, 0.15);
}

.text-input {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.7rem 0.75rem;
}

.text-input:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(184, 169, 144, 0.15);
}

/* --- Buttons --- */
.btn-run {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.95rem;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  padding: 0.85rem 1.5rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  letter-spacing: 0.01em;
}

.btn-run:hover {
  background: var(--accent-hover);
}

.btn-run:active {
  transform: scale(0.98);
}

.btn-run:disabled {
  background: var(--text-hint);
  cursor: not-allowed;
  transform: none;
}

.btn-icon {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem;
  cursor: pointer;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, border-color 0.2s;
}

.btn-icon:hover {
  color: var(--text);
  border-color: var(--border-focus);
}

.btn-secondary {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--text);
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.btn-secondary:hover {
  background: #fff;
  border-color: var(--border-focus);
}

.btn-secondary:disabled {
  color: var(--text-hint);
  cursor: not-allowed;
  background: rgba(255, 255, 255, 0.45);
}


.btn-text {
  font: inherit;
  color: var(--accent);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

.btn-text:hover {
  color: var(--accent-hover);
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-family: var(--font-body);
  font-weight: 500;
  font-size: 0.82rem;
  color: var(--accent);
  background: var(--accent-light);
  border: 1px solid rgba(194, 113, 79, 0.2);
  border-radius: 6px;
  padding: 0.5rem 1rem;
  cursor: pointer;
  margin-top: 0.75rem;
  transition: background 0.2s;
}

.btn-download:hover {
  background: rgba(194, 113, 79, 0.15);
}

/* --- Status --- */
.status-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-hint);
  flex-shrink: 0;
}

.status-dot.idle { background: var(--text-hint); }
.status-dot.working {
  background: var(--working);
  animation: pulse 1.2s ease-in-out infinite;
}
.status-dot.complete { background: var(--success); }
.status-dot.error { background: var(--error); }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.85); }
}

.status-text {
  font-weight: 600;
  font-size: 0.9rem;
}

.status-message {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

/* --- Description --- */
.description-editor {
  min-height: 12rem;
  font-family: var(--font-body);
  font-size: 0.85rem;
  line-height: 1.5;
  background: var(--accent-light);
}

.description-editor[readonly] {
  opacity: 0.9;
}

/* --- Empty state --- */
.empty-state {
  color: var(--text-hint);
  font-size: 0.82rem;
  font-style: italic;
  padding: 1rem 0;
  text-align: center;
}

/* --- Responsive --- */
@media (max-width: 900px) {
  .pipeline-flow {
    padding: 1rem;
  }

  .stage-marker {
    width: 2rem;
    height: 2rem;
    font-size: 0.8rem;
    right: 0.85rem;
  }

  .selection-toolbar {
    align-items: flex-start;
  }

  .selection-actions {
    width: 100%;
    justify-content: flex-start;
    margin-left: 0;
  }

  .pipeline-editor-toolbar {
    grid-template-columns: 1fr;
  }

  .pipeline-editor-create-row {
    flex-direction: column;
    align-items: stretch;
  }

  .stage-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .stage-select {
    width: 100%;
    min-width: 0;
  }
}
