:root {
  --bg: #121214;
  --surface: #1a1a1d;
  --surface-2: #202024;
  --surface-3: #28282d;
  --border: #2c2c30;
  --border-strong: #3a3a40;
  --text: #ececea;
  --text-secondary: #a5a5ab;
  --text-muted: #6d6d74;
  --accent: #e3a458;
  --accent-strong: #f0b877;
  --accent-bg: rgba(227, 164, 88, 0.12);
  --accent-border: rgba(227, 164, 88, 0.4);
  --danger: #e2665a;
  --danger-bg: rgba(226, 102, 90, 0.12);
  --danger-border: rgba(226, 102, 90, 0.4);
  --radius: 8px;
  --mono: "SF Mono", "Menlo", "Consolas", monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ---- Auth pages (unchanged narrow layout) ---- */
.wrap {
  max-width: 480px;
  margin: 0 auto;
  padding: 2rem 1.25rem 4rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 1.5rem;
  letter-spacing: 0.01em;
}

.brand .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

/* ---- App shell (two column) ---- */
.app-shell {
  display: grid;
  grid-template-columns: 340px 1fr;
  min-height: 100vh;
}

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.5rem;
  overflow-y: auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.tokens-pill {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  border-radius: 999px;
  padding: 4px 10px;
}

.main-panel {
  padding: 1.5rem 2rem;
  position: relative;
}

.section-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0 0 8px;
}

/* ---- Dropzone ---- */
.dropzone {
  background: var(--surface-2);
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 1.75rem 1rem;
  text-align: center;
  margin-bottom: 0.75rem;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.dropzone:hover, .dropzone.drag-over {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.dropzone p { margin: 0; font-size: 13px; }
.dropzone .primary-text { font-size: 14px; color: var(--text-secondary); margin-bottom: 2px; }
.dropzone .hint { color: var(--text-muted); font-size: 12px; }
.dropzone-preview { max-width: 100%; max-height: 140px; border-radius: var(--radius); }

.drag-overlay {
  position: fixed;
  inset: 0;
  background: rgba(227, 164, 88, 0.08);
  border: 3px dashed var(--accent);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--accent);
  pointer-events: none;
}

.resolution-info {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--mono);
  margin: 0 0 1rem;
}

.resolution-warning {
  font-size: 12px;
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin: 0 0 1rem;
}

/* ---- Pills ---- */
.pill-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.pill {
  font-size: 13px;
  padding: 6px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}

.pill.flex { flex: 1; text-align: center; }
.pill.selected { background: var(--accent-bg); color: var(--accent-strong); border-color: var(--accent-border); }
.pill:disabled { opacity: 0.4; cursor: not-allowed; }

.number-row { display: flex; gap: 8px; margin-bottom: 1.25rem; }
.number-btn {
  flex: 1;
  font-family: var(--mono);
  font-size: 14px;
  padding: 8px 0;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}
.number-btn.selected { background: var(--accent-bg); color: var(--accent-strong); border-color: var(--accent-border); }

/* ---- Inputs ---- */
input[type="text"], input[type="email"], input[type="password"], input[type="number"] {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-size: 14px;
  background: var(--surface-2);
  color: var(--text);
  width: 100%;
}

input::placeholder, textarea::placeholder { color: var(--text-muted); }

button { font-family: inherit; }

.seed-row { display: flex; gap: 8px; margin-bottom: 1.25rem; }
.seed-row input { flex: 1; }

.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
}

.icon-btn.active { background: var(--accent-bg); border-color: var(--accent-border); }

/* ---- Format toggle ---- */
.format-note {
  font-size: 11px;
  color: var(--text-muted);
  margin: -4px 0 1.25rem;
}

/* ---- Manual test override ---- */
.manual-test-box {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 1.25rem;
  background: var(--surface-2);
}
.manual-test-box textarea, .manual-test-box input { background: var(--surface-3); }

textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface-2);
  color: var(--text);
  resize: vertical;
  min-height: 90px;
  margin-bottom: 12px;
}

/* ---- Generate button + estimate ---- */
/* (cost estimate now lives inside the Generate button itself, see .btn-token-badge) */

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #1a1408;
  border: none;
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary .btn-token-badge {
  font-family: var(--mono);
  font-weight: 700;
  background: rgba(26, 20, 8, 0.18);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
}

.spinner {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid rgba(26, 20, 8, 0.3);
  border-top-color: #1a1408;
  animation: spin 0.7s linear infinite;
}

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

.error-banner {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 1rem;
}

.feedback-link {
  font-size: 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
}

/* ---- Main panel: preview / staged generation ---- */
.panel-title {
  font-size: 13px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 0 0 12px;
}

.preview-stage {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  min-height: 280px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 2rem;
}

.preview-stage.empty {
  color: var(--text-muted);
  font-size: 13px;
}

.preview-stage .staged-blur-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(24px) brightness(0.55);
  position: absolute;
  inset: 0;
}

.staged-log {
  position: absolute;
  top: 14px;
  left: 14px;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  border-radius: 6px;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent-strong);
  z-index: 2;
}

.staged-spinner-wrap {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.big-spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid rgba(227, 164, 88, 0.25);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  padding: 14px;
}

.result-card { text-align: center; }
.result-card img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--radius);
  margin-bottom: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.result-label { font-size: 11px; color: var(--text-muted); margin: 4px 0 6px; font-family: var(--mono); }

.result-actions { display: flex; gap: 6px; }
.result-actions button {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}

.result-actions .download-btn {
  flex: 1.4;
  font-weight: 600;
  background: var(--accent-bg);
  border-color: var(--accent-border);
  color: var(--accent-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.result-actions .edit-btn {
  background: var(--surface-2);
}

.result-actions button.thumb-up.selected { background: var(--accent-bg); border-color: var(--accent-border); color: var(--accent-strong); }
.result-actions button.thumb-down.selected { background: var(--danger-bg); border-color: var(--danger-border); color: var(--danger); }

/* ---- History ---- */
/* ---- History (grid, like Magnific's "latest generations") ---- */
.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.history-card {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.history-card img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}

.history-card .history-tag {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 6px 8px;
  font-size: 10px;
  font-family: var(--mono);
  color: var(--text);
  background: linear-gradient(to top, rgba(0,0,0,0.75), transparent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-card .history-actions {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.history-card:hover .history-actions { opacity: 1; }

.history-card .history-actions button,
.history-card .history-actions a {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: rgba(20,20,22,0.85);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  cursor: pointer;
  text-decoration: none;
}

.empty-state { text-align: center; color: var(--text-muted); font-size: 13px; padding: 2rem 0; }

/* ---- Modals ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  width: 90%;
  max-width: 380px;
}

.modal h3 { margin: 0 0 10px; font-size: 15px; }

.edit-modal { max-width: 460px; }
.edit-source-preview {
  width: 100%;
  max-height: 140px;
  object-fit: contain;
  border-radius: 6px;
  background: var(--surface-2);
  margin-bottom: 12px;
  border: 1px solid var(--border);
}

.reference-dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 10px;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  margin-bottom: 12px;
}
.reference-dropzone:hover { border-color: var(--accent); }

.reference-thumbs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.reference-thumbs img {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
}

.modal-actions { display: flex; gap: 8px; }
.modal-actions button { flex: 1; }

/* Lightbox */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 2rem;
}

.lightbox-back {
  position: absolute;
  top: 20px;
  left: 20px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}

.lightbox-overlay img {
  max-width: 100%;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 6px;
}

.hidden { display: none !important; }

/* ---- Prompt inspector ---- */
.prompt-modal { max-width: 720px; width: 92vw; }
.prompt-meta {
  font-size: 12px; color: var(--text-muted);
  margin-bottom: 10px; line-height: 1.6;
}
.prompt-meta strong { color: var(--text); font-weight: 600; }
.prompt-body {
  background: var(--surface-2, rgba(0,0,0,0.04));
  border: 1px solid var(--border, rgba(0,0,0,0.1));
  border-radius: 8px;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 42vh;
  overflow-y: auto;
  margin: 0 0 12px;
}
.prompt-params { max-height: 20vh; }
.result-actions button.prompt-btn, .history-actions button.prompt-btn { font-size: 12px; }
.history-filename {
  font-size: 11px; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 0 2px 4px;
}

/* ---- Added: contact link row, edit sizing, upscale ---- */

.footer-links {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 4px;
}
.footer-links .feedback-link { width: auto; }
.footer-sep { color: var(--text-muted); font-size: 12px; }

.hint-line {
  font-size: 11px;
  color: var(--text-muted);
  margin: 6px 0 0;
  line-height: 1.5;
}

/* Aspect ratio + resolution, side by side in the edit modal */
.edit-size-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.edit-size-row .section-label { margin-top: 14px; }

.modal select {
  width: 100%;
  box-sizing: border-box;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 9px 10px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
}
.modal select:focus {
  outline: none;
  border-color: var(--accent-border);
}

/* Upscale */
.upscale-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: 6px;
  padding: 5px 9px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}
.upscale-btn:hover {
  border-color: var(--accent-border);
  color: var(--accent);
}
.upscale-btn:disabled { opacity: 0.45; cursor: default; }

#upscale-scale-row .pill.disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

/* An upscale sits alongside the original rather than replacing it, so it
   needs to be visually distinguishable in the results and history grids. */
.upscale-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  color: var(--accent-strong);
  font-size: 10px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 20px;
  z-index: 2;
  backdrop-filter: blur(4px);
}
.result-card, .history-card { position: relative; }

.upscale-pending {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 120px;
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  text-align: center;
  padding: 12px;
}

.notice-banner {
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  color: var(--accent-strong);
  border-radius: var(--radius);
  padding: 9px 12px;
  font-size: 12px;
  line-height: 1.5;
  margin-bottom: 12px;
}

/* ---- Added: fit oversized outputs to the viewport ---- */

/* Upscales run to 16MP+. Native size meant the lightbox showed the top-left
   corner of the image and nothing else. Default to fitting the viewport;
   the zoom button switches to 1:1 with scrolling for detail inspection. */
#lightbox-img {
  max-width: 92vw;
  max-height: 88vh;
  width: auto;
  height: auto;
  object-fit: contain;
  cursor: zoom-in;
}
#lightbox-img.zoomed {
  max-width: none;
  max-height: none;
  cursor: grab;
}
.lightbox-overlay {
  overflow: auto;
  align-items: flex-start;
  justify-content: flex-start;
}
.lightbox-overlay:not(:has(.zoomed)) {
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.lightbox-zoom {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}
.lightbox-zoom:hover { border-color: var(--accent-border); color: var(--accent); }

/* Result and history thumbnails: never let a tall portrait render stretch the
   grid row to its own height. */
.result-card img {
  max-height: 60vh;
  object-fit: contain;
  width: 100%;
}
.history-card img {
  aspect-ratio: 4 / 3;
  object-fit: cover;
  width: 100%;
}
